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