Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Process.Start throw Win32Exception if using different user credentials?

Tags:

c#

process

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 info = 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!

like image 654
Sean Gough Avatar asked Dec 18 '25 01:12

Sean Gough


1 Answers

Is your Secondary Logon service started, I believe this is required to start a new process under a different user account?

like image 187
CodingGorilla Avatar answered Dec 19 '25 14:12

CodingGorilla



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!