C # Analog Domain logon

Source: Internet
Author: User

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&gt
        ; 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)

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.