Impersonating a Windows user

前端 未结 3 2058
自闭症患者
自闭症患者 2020-11-30 07:46

I am using the code to impersonate a user account to get access to a file share.

public class Impersonator :
    IDisposable
{
    #region Public methods.
           


        
3条回答
  •  独厮守ぢ
    2020-11-30 08:31

    try this :

    [DllImport("advapi32.dll", SetLastError = true)]
        public static extern bool LogonUser(
                string lpszUsername,
                string lpszDomain,
                string lpszPassword,
                int dwLogonType,
                int dwLogonProvider,
                out IntPtr phToken);
    

    Usage :

    IntPtr userToken = IntPtr.Zero;
    
    bool success = External.LogonUser(
      "john.doe", 
      "domain.com", 
      "MyPassword", 
      (int) AdvApi32Utility.LogonType.LOGON32_LOGON_INTERACTIVE, //2
      (int) AdvApi32Utility.LogonProvider.LOGON32_PROVIDER_DEFAULT, //0
      out userToken);
    
    if (!success)
    {
      throw new SecurityException("Logon user failed");
    }
    
    using (WindowsIdentity.Impersonate(userToken))
    {
     Process.Start("explorer.exe", @"/root,\\server01-Prod\abc");
    }
    

提交回复
热议问题