Domain logon validation. NET Edition

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.