.NET 4 : Process. 开始使用凭据返回空输出

标签 .net .net-4.0 process.start

我从 运行一个外部程序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 32 位

更新:

该应用程序在 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 是外部应用程序的名称。

最佳答案

我意识到这是不久前被问到的,但我遇到了同样的问题并在这个网站上找到了解决方案:The Perils and Pitfalls of Launching a Process Under New Credentials

在应用程序无法正确初始化部分下应用解决方案为我修复了它。

希望这会节省一些其他人的时间和挫折!

关于.NET 4 : Process. 开始使用凭据返回空输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2811937/

相关文章:

c# - 为什么 Thread.Sleep( ... ) 避免 CPU 使用基于多线程 Windows 服务的应用程序的 100% 重执行?

c# - 数据集返回意外结果的 JsonConvert.serializeObject

c# - 找不到 Column[0] 运行时错误

sqlite - 什么是混合模式装配?

c# - Process.Start() 继续运行我的 .exe 作为后台进程 MVC ASP.Net

.net - .NET中的内存泄漏

具有可选参数的 C# 递归函数

c# - 如何将附加行为右键单击应用于文本 block ?

c# - 如何禁用下载提示,如 'do you want to open or save or save_as?'

c# - Process.Start打开多个网页