【问题标题】:.NET 4: Process.Start using credentials returns empty output.NET 4:Process.Start 使用凭据返回空输出
【发布时间】:2011-02-18 05:01:27
【问题描述】:

我从 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() 返回一个空字符串。不会抛出异常。

userNamesecurePassword 是正确的(如果凭据不正确,则会引发异常)。

如何在另一个用户帐户的上下文中运行程序?

环境: .NET 4,Windows Server 2008 32bit

UPD:

该应用程序在 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,应用程序启动时间 0x01caf1b99

cryptcp.exe 是外部应用程序的名称。

【问题讨论】:

  • 另一方面,这个问题stackoverflow.com/questions/2345620/…,通过搜索相同的故障偏移和kernel32.dll 可能表明一旦在IIS 下运行,这是一个无法克服的问题 - cryptcp.exe一定想以某种方式与桌面交互(根据我的阅读)

标签: .net .net-4.0 process.start


【解决方案1】:

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

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

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

【讨论】:

  • 通过这个,你的意思是修复是什么?链接的网站已关闭。
【解决方案2】:

您启动的应用程序可能需要加载它的配置文件(默认情况下不会)。您是否尝试将 LoadUserProfile 属性设置为 true?

【讨论】:

    【解决方案3】:

    According to Microsoft,你不能像这样读取标准输出和标准错误,因为它会导致死锁。要解决此问题,请使用以下内容:

    private readonly StringBuilder outputText = new StringBuilder();
    private readonly StringBuilder errorText = new StringBuilder();
    

    。 . .

            process.OutputDataReceived += delegate(
                object sendingProcess,
                DataReceivedEventArgs outLine)
            {
                if (!string.IsNullOrEmpty(outLine.Data))
                {
                    outputText.AppendLine(outLine.Data);
                }
            };
    
            process.ErrorDataReceived += delegate(
                object sendingProcess,
                DataReceivedEventArgs errorLine)
            {
                if (!string.IsNullOrEmpty(errorLine.Data))
                {
                    errorText.AppendLine(errorLine.Data);
                }
            };
    
            process.BeginOutputReadLine();
            process.BeginErrorReadLine();
            process.WaitForExit();
            Console.WriteLine(errorText.ToString());
            Console.WriteLine(outputText.ToString());
    

    【讨论】:

    • 谢谢,很好的评论。但是这个问题解决了另一个问题。我将评论错误输出重定向。
    【解决方案4】:

    查看MSDN documentation,建议配置其他一些项目以作为另一个用户正确启动应用程序。

    1. 设置域、用户名和密码属性(您应该设置域)
    2. 将工作目录设置为默认使用用户名/密码,它是 system32 文件夹

    这可能会帮助您解决这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      • 2016-12-14
      • 2020-10-13
      • 2022-09-27
      • 2014-12-05
      • 2019-11-10
      相关资源
      最近更新 更多