Using system;using system.data;using system.configuration;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; necessary reference using System.Security.Principal; Necessary references//<summary>///userloginfordomain Summary description///</summary>public class Userloginfordomain{public Userloginfordomain () {////todo: Add constructor logic here//} public const int logon32_logon_interactive = 2; public const int logon32_provider_default = 0; WindowsImpersonationContext impersonationcontext; [DllImport ("advapi32.dll", CharSet = CharSet.Auto)] public static extern int LogonUser (String lpszUserName, String lpszdomain, String Lpszpassword, int dwLogonType, int dwlogonprovider, Ref IntPtr phtoken); [DllImport ("advapi32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)] public extern s tatic int DuplicateToken (IntPtr htoken, int impersonationlevel, Ref INTPTR hNewToken); /**///<summary>///Enter user name, password, login domain to determine success///</summary>//<example>//if (Impersonat Evaliduser (UserName, Domain, Password)) {}//</example>//<param name= "UserName" > account name, e.g. string Userna me = usernametextbox.text;</param>//<param name= "domain" > domain to log in, such as: string domain = DOMAINTEXTBOX.TEXT;&L t;/param>//<param name= "password" > account password, e.g. string password = passwordtextbox.text;</param>//< Returns> returns true successfully, otherwise false</returns> public bool impersonateValidUser (string userName, string domain, string Password) {WindowsIdentity tempwindowsidentity; INTPTR token = IntPtr.zero; IntPtr tokenduplicate = IntPtr.Zero; if (LogonUser (userName, domain, password, logon32_logon_interactive, Logon32_provider_default, ref token)! = 0) {if (DuplicateToken (token, 2, ref tokenduplicate)! = 0) {tempwindowsidentity = New WindowsIdentity (tokenduplicate); impersonationcontext = Tempwindowsidentity.impersonate (); if (impersonationcontext! = null) return true; else return false; } else return false; } else return false; } public void Undoimpersonation () {Impersonationcontext.undo (); }}
protected void okButton_Click (object sender, EventArgs e) { string UserName = Usernametextbox.text; string Domain = Domaintextbox.text; string Password = Passwordtextbox.text; Userloginfordomain checkuserlogin = new Userloginfordomain (); if (Checkuserlogin.impersonatevaliduser (UserName, Domain, Password)) Response.Write (Usernametextbox.text + "is OK "); else Response.Write (Usernametextbox.text + "is Error"); }
<div> Account: <asp:textbox id= "Usernametextbox" runat= "server" ></asp:textbox><br/> Password: <asp:textbox id= "Passwordtextbox" runat= "server" ></asp:textbox><br/> domain name: <asp:textbox Id= "Domaintextbox" runat= "server" ></asp:textbox><br/> <asp:button id= "OKButton" runat= " Server "onclick=" okButton_Click "text=" button "/></div>
Domain logon validation. NET Edition