I need to start a process as another user and it's bombing.
I scaled it down my code to a simple reference example. This code works fine to start the process itself:
var i开发者_运维知识库nfo = new ProcessStartInfo("notepad.exe")
{
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardError = true,
RedirectStandardOutput = true
};
However, if I add the UserName
and Password
values:
var info = new ProcessStartInfo("notepad.exe")
{
UserName = "user",
Password = StringToSecureString("password"),
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardError = true,
RedirectStandardOutput = true
};
Process.Start(info);
It bombs with the ever so helpful System.ComponentModel.Win32Exception message:
The service cannot be started, either because it is disabled or because it has no enabled devices associated with it.
Just in case, here is the secure string conversion method:
private static SecureString StringToSecureString(string s)
{
var secure = new SecureString();
foreach (var c in s.ToCharArray())
{
secure.AppendChar(c);
}
return secure;
}
Any ideas or alternate solutions would be very much appreciated!
Is your Secondary Logon
service started, I believe this is required to start a new process under a different user account?
I think you are probably missing the Domain
property on the ProcessStartInfo
.
Compare your code to the example given in the docs:
Imports System
Imports System.ComponentModel
Imports System.Diagnostics
Imports System.Security
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Security;
public class Example
{
public static void Main()
{
// Instantiate the secure string.
SecureString securePwd = new SecureString();
ConsoleKeyInfo key;
Console.Write("Enter password: ");
do {
key = Console.ReadKey(true);
// Ignore any key out of range.
if (((int) key.Key) >= 65 && ((int) key.Key <= 90)) {
// Append the character to the password.
securePwd.AppendChar(key.KeyChar);
Console.Write("*");
}
// Exit if Enter key is pressed.
} while (key.Key != ConsoleKey.Enter);
Console.WriteLine();
try
{
Process.Start("Notepad.exe", "MyUser", securePwd, "MYDOMAIN");
}
catch (Win32Exception e)
{
Console.WriteLine(e.Message);
}
}
}
精彩评论