2013-04-30 85 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课程。

相关问题