This article mainly introduced the C # implementation of the Windows login function, combined with the example form analysis of the simple Windows graphical landing function implementation skills, the need for friends can refer to the following
The example in this article describes the Windows sign-in functionality implemented by C #. Share to everyone for your reference, as follows:
Using system;using system.data;using system.configuration;using system.collections;using System.Web;using System.web.security;using system.web.ui;using system.web.ui.webcontrols;using System.Web.UI.WebControls.WebParts; Using system.web.ui.htmlcontrols;using system.runtime.interopservices;using System.security.principal;namespace yutest{public partial class _default:system.web.ui.page {[DllImport ("advapi32.dll", CharSet = CharSet.Auto)] P ublic static extern bool LogonUser (string lpszusername,string lpszdomain,string lpszpassword,int dwlogontype,int dwlogonprovider,out int phtoken); protected void Page_Load (object sender, EventArgs e) {string AAA = System.Threading.Thread.CurrentPrincipal.Ident ity. Name; string bbb = SYSTEM.THREADING.THREAD.CURRENTPRINCIPAL.IDENTITY.N; System.Environment.UserDomainName//system.environment.username} protected void Button1_Click (object sender, System.EventArgs e) {//Verify that the user's input is empty if (Tdomain.texT.trim (). Length > 0 && tUserName.Text.Trim (). Length > 0&& TPassword.Text.Trim (). Length > 0) {//Call function login (string UserName, String Password, String Domain)//Implement Windows logon if (login ( TUserName.Text.Trim (), TPassword.Text.Trim (), TDomain.Text.Trim ()) = = True) {//Display login success Information Loginmsg.text = " Login Success!!! "; Loginmsg.visible = true; Return } else {//Display login failure Information Loginmsg.text = "Login failed, please reenter user name, password and system domain name!!!" "; Loginmsg.visible = true; }}} private bool Login (string UserName, String Password, String domain) {//Get user name and system domain name string tex T1 = Domain.trim (); String text2 = Username.trim (); Text2 = Text2. Replace ("/", @ "\"); Processing symbol "/" int num1 = Text2. IndexOf (' \ \ '); Gets the index of the symbol "\" if (num1! =-1) {//format user name and system domain Name Text1 = text2. Substring (0, NUM1); Text2 = Text2. Substring (NUM1 + 1); } else {//format user name and system domain name NUM1 = Text2. IndexOf (' @ '); if (num1! =-1) {Text1 = Text2. Substring (NUM1 + 1); Text2 = Text2. Substring (0, NUM1); }}//Call function Authenticateuser () to implement user Windows login return authenticateuser (Text2, Password.trim (), Text1); } private bool Authenticateuser (string UserName, String password,string Domain) {//flag for user logon success = BOOL Flag 1 = false; try {int num1; IntPtr ptr1; Call the Windows Login API if (! LogonUser (UserName, Domain, Password, 2, 0, out NUM1)) {//returns the login result return flag1; }//Call. NET Windows logon Ptr1 = new IntPtr (NUM1); WindowsIdentity identity1 = new WindowsIdentity (PTR1); WindowsPrincipal principal1 = new WindowsPrincipal (identity1); HttpContext.Current.User = Principal1; Set the system cookie and redirect page Formsauthentication.setauthcookie (Principal1. Identity.name, false); FormsAuthentication.RedirectFromLoginPage (UserName, false); Flag1 = true; } catch (Exception) {} return Flag1; } }}