How to use LogonUser properly to impersonate domain user from workgroup client

前端 未结 6 1893
走了就别回头了
走了就别回头了 2020-12-02 09:44

ASP.NET: Impersonate against a domain on VMWare

This question is what I am asking, but the answer does not provide details on how the _token is derived. It seems to

6条回答
  •  天命终不由人
    2020-12-02 10:34

    this works for me, full working example (I wish more people would do this):

    //logon impersonation
    using System.Runtime.InteropServices; // DllImport
    using System.Security.Principal; // WindowsImpersonationContext
    using System.Security.Permissions; // PermissionSetAttribute
    
    ...
    
    class Program {
    
        // obtains user token
        [DllImport("advapi32.dll", SetLastError = true)]
        public static extern bool LogonUser(string pszUsername, string pszDomain, string pszPassword,
            int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
    
        // closes open handes returned by LogonUser
        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        public extern static bool CloseHandle(IntPtr handle);
    
        public void DoWorkUnderImpersonation() {
            //elevate privileges before doing file copy to handle domain security
            WindowsImpersonationContext impersonationContext = null;
            IntPtr userHandle = IntPtr.Zero;
            const int LOGON32_PROVIDER_DEFAULT = 0;
            const int LOGON32_LOGON_INTERACTIVE = 2;
            string domain = ConfigurationManager.AppSettings["ImpersonationDomain"];
            string user = ConfigurationManager.AppSettings["ImpersonationUser"];
            string password = ConfigurationManager.AppSettings["ImpersonationPassword"];
    
            try {
                Console.WriteLine("windows identify before impersonation: " + WindowsIdentity.GetCurrent().Name);
    
                // if domain name was blank, assume local machine
                if (domain == "")
                    domain = System.Environment.MachineName;
    
                // Call LogonUser to get a token for the user
                bool loggedOn = LogonUser(user,
                                            domain,
                                            password,
                                            LOGON32_LOGON_INTERACTIVE,
                                            LOGON32_PROVIDER_DEFAULT,
                                            ref userHandle);
    
                if (!loggedOn) {
                    Console.WriteLine("Exception impersonating user, error code: " + Marshal.GetLastWin32Error());
                    return;
                }
    
                // Begin impersonating the user
                impersonationContext = WindowsIdentity.Impersonate(userHandle);
    
                Console.WriteLine("Main() windows identify after impersonation: " + WindowsIdentity.GetCurrent().Name);
    
                //run the program with elevated privileges (like file copying from a domain server)
                DoWork();
    
            } catch (Exception ex) {
                Console.WriteLine("Exception impersonating user: " + ex.Message);
            } finally {
                // Clean up
                if (impersonationContext != null) {
                    impersonationContext.Undo();
                }
    
                if (userHandle != IntPtr.Zero) {
                    CloseHandle(userHandle);
                }
            }
        }
    
    
        private void DoWork() {
            //everything in here has elevated privileges
    
            //example access files on a network share through e$ 
            string[] files = System.IO.Directory.GetFiles(@"\\domainserver\e$\images", "*.jpg");
        }
    }
    

提交回复
热议问题