Launch a process under another user's credentials

后端 未结 3 1094
一向
一向 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: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;
        }
    

提交回复
热议问题