我从ASP.NET运行一个外部程序:
var process = new Process();
var startInfo = process.StartInfo;
startInfo.FileName = filePath;
startInfo.Arguments = arguments;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
//startInfo.RedirectStandardError = true;
process.Start();
process.WaitForExit();
Console.Write("Output: {0}", process.StandardOutput.ReadToEnd());
//Console.Write("Error Output: {0}", process.StandardError.ReadToEnd());
这段代码一切正常:执行外部程序并且process.StandardOutput.ReadToEnd()返回正确的输出。
但是在我在process.Start()之前添加这两行之后(在另一个用户帐户的上下文中运行程序):
startInfo.UserName = userName;
startInfo.Password = securePassword;
该程序未执行,并且process.StandardOutput.ReadToEnd()返回一个空字符串。不抛出异常。
userName和securePassword是正确的(如果凭据不正确,则会引发异常)。
如何在另一个用户帐户的上下文中运行程序?
环境: .NET 4,Windows Server 2008 32bit
升级版:
该应用程序在 ASP.NET 开发服务器 + Windows 7 下运行良好,但在 IIS 7 + Windows Server 2008 Web 版上失败。
UPD2:
在事件日志中发现:
错误应用程序 cryptcp.exe,版本 3.33.0.0,时间戳 0x4be18460,错误模块 kernel32.dll,版本 6.0.6002.18005,时间戳 0x49e03821,异常代码 0xc0000142,错误偏移量 0x00009eed,进程 ID 0xbf4,应用程序启动时间 0x01caf1b91af5。
cryptcp.exe 是外部应用程序的名称。