2013-04-30 86 views
8

我試圖從我的Asp.Net Web應用程序執行PSExec以連接到遠程服務器。不知何故,它給"Access Denied Error -5"沒有設置憑據,並通過在PSEXEC命令中設置憑據"2250 Network connection could not be found"。我是服務器上的管理員,我啓用了Windows authentication and Asp.Net ImpersonationIIS7.5)。更有趣的是,當我嘗試從console application或甚至僅使用command prompt執行此操作時,它只是正常工作。我正在嘗試做一個ping操作作爲測試。從Asp.Net Web應用程序使用sysinternals PSExec執行腳本

這裏是我的代碼片段: -

  var startInfo = new ProcessStartInfo{ 
       CreateNoWindow = true, 
       UseShellExecute = false, 
       FileName = FilePath, 
       Arguments = CommandArgs 
      } 

      Process vsCommandProcess = Process.Start(startInfo); 

      vsCommandProcess.WaitForExit(); 
      var exitCode = vsCommandProcess.ExitCode; 
      if (vsCommandProcess.ExitCode != 0) 
      { 
       ...rest of the code 

這裏: -

FilePath --> C:\pstools\psexec.exe 
Arguments --> \\servername -accepteula -u domain\userName -p password ipconfig (1) 
       \\servername -accepteula ipconfig (2)   

(1) Gives Error 2250 (2) gives Error 5 

同樣的命令和代碼與控制檯應用程序工作。所以我相信這絕對是與Asp.net應用程序有關,它不能將憑證帶到機器的遠程。我的事件嘗試startInfo.LoadUserProfile但無濟於事。

感謝您的幫助。我試圖尋找類似的問題,但無法找到我面臨的問題的解決方案。

+0

1如果它與控制檯一起使用,而不是asp Web應用程序,這聽起來像是一個安全問題。 2你是否還檢查.net版本是否正確?嘗試使用.net 2.0在IIS上運行應用程序池(我今天遇到了這個問題)。 – gartenabfall 2013-04-30 19:05:53

+0

我正在使用dotnet 4.0版。是的,它似乎更多的身份驗證令牌沒有結轉...... – PSL 2013-04-30 19:07:20

+0

IIS是否以與命令提示符相同的用戶權限運行? – Powerslave 2013-05-09 21:28:05

回答

2

考慮將psexec帶出該過程。要WMI直接可能會更深入地瞭解了什麼錯誤:

var connOpts = new ConnectionOptions() 
{ 
// Optional, default is to use current identity 
    Username = "username", 
    Password = "password" 
}; 

// create a handle to the Win32_Process object on the remote computer 
ManagementScope mgmtScope = new ManagementScope(@"\\servername\root\cimv2", connOpts); 
ManagementClass w32Process = new ManagementClass(mgmtScope, 
    new ManagementPath("Win32_Process"), new ObjectGetOptions()); 

// create the process itself 
object[] createArgs = new object[] { 
    // [in] string CommandLine, 
    "notepad.exe", 
    // [in] string CurrentDirectory, 
    null, 
    // [in] Win32_ProcessStartup ProcessStartupInformation, 
    null, 
    // [out] uint32 ProcessId 
    0}; 

var result = (int)w32Process.InvokeMethod("Create", createArgs); 

switch (result) 
{ 
    case 0: /* no-op, successful start */ break; 
    case 2: throw new Exception("Access Denied"); 
    case 3: throw new Exception("Insufficient Privilege"); 
    case 8: throw new Exception("Unknown failure"); 
    case 9: throw new Exception("Path not found"); 
    case 21: throw new InvalidOperationException("Invalid Parameter"); 
} 

如果仍然遇到與假冒的問題,它可能有助於傾倒的HttpContext.Current.User.Identity內容,驗證正確配置IIS。此外,如果您正在使用Kerberos(通過Negotiate/SPNEGO),則可能需要allow the machine to delegate identity。如果您知道機器將連接到的SPN,則可以使用受限委派,但在許多情況下,如果目標未提前知道,則必須允許無約束委派。


注意:如果你是遠程處理的計算機只是運行ipconfig,你可以通過WMI相同的信息,而不必惹試圖讓STDOUT輸出回調用計算機。看看Win32_NetworkAdapterConfiguration課程。

相關問題