2010-11-09 21 views
3

我需要以另一個用戶的身份啓動一個進程,並進行轟炸。如果使用不同的用戶憑據,Process.Start爲什麼會拋出Win32Exception?

我將它縮小到一個簡單的參考示例。此代碼工作正常啓動過程本身:

 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; 
    } 

任何意見或替代解決方案將非常感謝!

+0

您的代碼爲我工作,所以這個問題是不符合實際的代碼。 – 2010-11-09 15:09:55

回答

5

您的Secondary Logon服務是否已啓動,我相信這是在不同用戶帳戶下啓動新流程所必需的嗎?

+0

哦,它總是很簡單,不是嗎?謝謝!!! – 2010-11-09 15:20:45

+0

給出的是,有錯誤告訴你它正在尋找一個無法啓動的「服務」。 ;) – CodingGorilla 2010-11-09 15:22:05

+0

看,你說「放棄」,我說「混亂如地獄」。 ;) – 2010-11-09 15:34:26

0

我想你可能會錯過ProcessStartInfo上的Domain屬性。

你的代碼進行比較,以在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); 
     } 
    } 
} 
+0

對不起,我可能應該指定我也嘗試過使用域名。同樣的結果。 – 2010-11-09 15:18:48

+0

FWIW,一旦我啓用了輔助登錄服務,它在沒有域的情況下工作。 – 2010-11-09 15:21:28

相關問題