C # impersonate a domain login

Source: Internet
Author: User

If you want to use C # for background domain logons, you need to use this assembly to Advapi32.dll. Advapi32.dll is part of a high-level API Application Interface service library that contains functions related to object security, registry manipulation, and event logs. XP system is generally located in the C:\WINDOWS\system32\ directory, size 659KB.

Here is the code that implements the domain logon:

public class Simulatedomainservice {public static bool impersonateValidUser (string userName, string domain, STR            ing password) {//servicecontext.setthreadprincipal ();            WindowsImpersonationContext impersonationcontext;            WindowsIdentity tempwindowsidentity;            IntPtr token = IntPtr.Zero;            IntPtr tokenduplicate = IntPtr.Zero;                    if (RevertToSelf ()) {if (Logonusera (userName, domain, password, logon32_logon_interactive,  Logon32_provider_default, ref token)! = 0) {if (DuplicateToken (token, 2, Ref tokenduplicate)! = 0) {tempwindowsidentity = new WindowsIdentity (TOKENDUPL                        Icate);                        impersonationcontext = Tempwindowsidentity.impersonate ();               if (impersonationcontext! = null) {CloseHandle (token);             CloseHandle (tokenduplicate);                        return true; }}}} if (token! = IntPtr.Zero) CloseHandle (token            );            if (tokenduplicate! = IntPtr.Zero) CloseHandle (tokenduplicate);        return false;        }//<summary>//Demo login///</summary>//<returns></returns> public static bool impersonateValidUser () {return impersonateValidUser (GetValue ("account"), GetValue ("        Domain "), GetValue (" pwd ")); }///<summary>//Read the value of the specified key///</summary>//<param name= "key" >            </param>//<returns></returns> public static string GetValue (String key) { Return System.configuration.configurationmanager.appsettings[key].        ToString (); } public static CustomReportcredentials getreportcredentials (String account,string domain,string pwd) {return new Customrepor        Tcredentials (account, pwd, domain);        }///<summary>///for reporting certification///</summary>//<returns></returns> public static Customreportcredentials Getreportcredentials () {return getreportcredentials (GetValue        ("Account"), GetValue ("domain"), GetValue ("pwd")); } #region Win32 API Extend method [DllImport ("advapi32.dll")] static extern int Logonusera (String LPs Zusername, String lpszdomain, String lpszpassword, int dwlogontype, int Dwlogo        Nprovider, ref IntPtr phtoken); [DllImport ("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)] static extern int DuplicateToken (INTPTR HT        Oken, int impersonationlevel, ref IntPtr hNewToken); [DllImport ("Advapi32.dll", CharSet= CharSet.Auto, SetLastError = true)] static extern bool RevertToSelf ();        [DllImport ("kernel32.dll", CharSet = CharSet.Auto)] static extern bool CloseHandle (INTPTR handle);        const int logon32_logon_interactive = 2;        const int logon32_provider_default = 0; #endregion}

which
impersonateValidUser
This method has three parameters, namely, account name, domain name, password. The return value is type Boole, which indicates that the login was successful or the login failed if it returns true.

Note: The server needs to be within the domain name of the impersonated login (belonging to the same domain)

C # impersonate a domain login

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.