Impersonating a Windows user

前端 未结 3 2068
自闭症患者
自闭症患者 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:26

    Instead of using your Impersonator class, what happens when you call Process.Start and pass in a ProcessStartInfo instance that contains the username, password and domain that you want to run the process as?

    Perhaps, if that works, then your Impersonator class should create a ProcessStartInfo instance and use that to create new processes (encapsulate that within the class itself).

    var psi = new ProcessStartInfo("explorer.exe", @"/root,\\server01-Prod\abc");
    psi.Domain = domain;
    psi.UserName = username;
    psi.Password = password;
    psi.WorkingDirectory = workingDir;
    
    Process.Start(psi);
    

    Also, per the MSDN docs...

    Setting the Domain, UserName, and the Password properties in a ProcessStartInfo object is the recommended practice for starting a process with user credentials.

    You should also set the working directory when starting a process with different user creds.

提交回复
热议问题