If you want to use C # for background domain logons, you need to use the Advapi32.dll assembly. Advapi32.dll is part of an Advanced API Application Interface service library that contains functions related to object security, registry manipulation, and event logging. XP system is generally located in the C:\WINDOWS\system32\ directory, the size of 659KB.
Here is the code that implements the domain login:
public class Simulatedomainservice {public static bool impersonateValidUser (string userName, string domain, S
Tring 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 (toke N, 2, ref tokenduplicate)!= 0) {tempwindowsidentity = new WindowsIdentity (to
Kenduplicate);
impersonationcontext = Tempwindowsidentity.impersonate (); if (impersonationcontext!= null) {CloseHandle (token);
CloseHandle (tokenduplicate);
return true; The IF (token!= intptr.zero) CloseHandle (
token);
if (tokenduplicate!= IntPtr.Zero) CloseHandle (tokenduplicate);
return false;
}///<summary>///analog login///</summary>///<returns></returns> public static bool impersonateValidUser () {return impersonateValidUser (GetValue (' account '), get
Value ("domain"), GetValue ("pwd")); ///<summary>///reads 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 to new Customreportcredentials (account, pwd, domain); ///<summary>///Obtain report authentication information///</summary>///<returns></returns>
; public static Customreportcredentials Getreportcredentials () {return getreportcredentials (GetValue ("a
Ccount "), GetValue (" domain "), GetValue (" pwd ")); #region Win32 API Extend method [DllImport ("advapi32.dll")] static extern int Logonusera (String lpszUserName, String lpszdomain, String lpszpassword, int dwlogontype, int
dwLogonProvider, ref IntPtr phtoken); [DllImport ("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)] static extern int DuplicateToken (IntPtr h
Token, 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 the Boole type, and if true, the login succeeds or the login fails.
Note: The server needs to be within the domain name of the impersonated login (belonging to the same domain)