Launch a process under another user's credentials

后端 未结 3 1083
一向
一向 2020-11-29 09:39

I want to launch a process under another username\'s credentials. This is what I have now:

        /// 
        /// Do actions under another          


        
相关标签:
3条回答
  • 2020-11-29 10:12

    I don't think you need to login the user and duplicate the handle. All you need to do is set the username, domain, and password in the ProcessStartInfo object. That's been available since .NET 3.0 if I recall correctly.

    EDIT:

    Also for more information, this is what really happens when you give username/domain/password to ProcessStartInfo: http://support.microsoft.com/kb/165194. The reason for access denied is probably that you don't have access to the user's desktop or windowstation. Just calling LoginUser is not sufficient.

    0 讨论(0)
  • 2020-11-29 10:15

    May be you forgot to make read only secure string? My method works fine for me:

    Process proc = new Process
            {
                StartInfo =
                {
                    CreateNoWindow = true,
                    UseShellExecute = false,
                    RedirectStandardOutput = true,
                    RedirectStandardInput = true,
                    FileName = "cmd.exe",
                    UserName = "user",
                    Domain = "myDomain",
                    Password = GetSecureString("Password"),
                    Arguments = "/c ipconfig"                   
                }
            };
            proc.Start(); 
    

    GetSecureString() function:

    public static SecureString GetSecureString(string str)
        {
            SecureString secureString = new SecureString();
            foreach (char ch in str)
            {
                secureString.AppendChar(ch);
            }
            secureString.MakeReadOnly();
            return secureString;
        }
    
    0 讨论(0)
  • 2020-11-29 10:28

    It might be weird for some people, my apologies, I am Linux developer... Tested:

        /// <summary>
        /// Class that deals with another username credentials
        /// </summary>
        class Credentials
        {
            /// <summary>
            /// Constructor of SecureString password, to be used by RunAs
            /// </summary>
            /// <param name="text">Plain password</param>
            /// <returns>SecureString password</returns>
            private static SecureString MakeSecureString(string text)
            {
                SecureString secure = new SecureString();
                foreach (char c in text)
                {
                    secure.AppendChar(c);
                }
    
                return secure;
            }
    
            /// <summary>
            /// Run an application under another user credentials.
            /// Working directory set to C:\Windows\System32
            /// </summary>
            /// <param name="path">Full path to the executable file</param>
            /// <param name="username">Username of desired credentials</param>
            /// <param name="password">Password of desired credentials</param>
            public static void RunAs(string path, string username, string password)
            {
                try
                {
                    ProcessStartInfo myProcess = new ProcessStartInfo(path);
                    myProcess.UserName = username;
                    myProcess.Password = MakeSecureString(password);
                    myProcess.WorkingDirectory = @"C:\Windows\System32";
                    myProcess.UseShellExecute = false;
                    Process.Start(myProcess);
                }
                catch (Win32Exception w32E)
                {
                    // The process didn't start.
                    Console.WriteLine(w32E);
                }
            }
    
        }
    
    0 讨论(0)
提交回复
热议问题