如果使用不同的用户凭据,为什么Process.Start会抛出Win32Exception?

时间:2010-11-09 15:02:35

标签: c# process

我需要以另一个用户身份启动一个流程,这是一次轰炸。

我将我的代码缩减为一个简单的参考示例。此代码可以正常启动进程:

        var info = new ProcessStartInfo("notepad.exe")
                       {
                           UseShellExecute = false,
                           RedirectStandardInput = true,
                           RedirectStandardError = true,
                           RedirectStandardOutput = true
                       };

但是,如果我添加UserNamePassword值:

       var info = new ProcessStartInfo("notepad.exe")
                       {
                           UserName = "user",
                           Password = StringToSecureString("password"),
                           UseShellExecute = false,
                           RedirectStandardInput = true,
                           RedirectStandardError = true,
                           RedirectStandardOutput = true
                       };
        Process.Start(info);

它以极其有用的System.ComponentModel.Win32Exception消息轰炸:

  

无法启动该服务,因为它已被禁用,或者因为它没有与之关联的已启用设备。

以防万一,这是安全字符串转换方法:

    private static SecureString StringToSecureString(string s)
    {
        var secure = new SecureString();
        foreach (var c in s.ToCharArray())
        {
            secure.AppendChar(c);
        }
        return secure;
    }

非常感谢任何想法或替代解决方案!

2 个答案:

答案 0 :(得分:5)

您的Secondary Logon服务是否已启动,我认为需要在其他用户帐户下启动新流程?

答案 1 :(得分:0)

我认为您可能错过Domain上的ProcessStartInfo属性。

将您的代码与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);
        }
    }
}