【问题标题】:System.Diagnostics.Process not accepting credentialsSystem.Diagnostics.Process 不接受凭据
【发布时间】:2011-12-19 19:46:30
【问题描述】:

我正在尝试使用 Process 类执行批处理文件。此代码位于我使用 LogonUser() 和 WindowsIdentity.Impersonate() 模拟本地 PC 管理员帐户的较大部分代码的中间。

我试图在 ProcessStartInfo 中运行批处理文件,但没有在 ProcessStartInfo 中添加凭据,但这样做会导致批处理文件静默失败 - 没有引发错误,并且从未返回批处理文件的预期输出(我正在异步读取 stderr 和 stdout,fwiw)。

然后我将凭据添加到 ProcessStartInfo,但现在如果我不先调用 WindowsImpersonationContext.Undo(),我会收到“访问被拒绝”错误,如果我这样做,则会收到“登录失败:未知用户名或密码错误”错误在 Process.Start() 之前调用 .Undo()。我已经三次检查了多个帐户的用户名/密码/域是否正确。

如果我的代码没有 LogonUser() 或 WindowsIdentity.Impersonate() 调用(并且 ProcessStartInfo 中没有凭据),那么我在执行批处理文件和从批处理文件中捕获的输出方面没有问题。

我能够以本地管理员或任意本地用户帐户的身份从桌面成功运行批处理文件。我可以看到它的权限表明它应该可以从我尝试运行它的帐户中读取/可执行。这真的很困难。任何帮助表示赞赏。

【问题讨论】:

    标签: process permissions credentials processstartinfo impersonation


    【解决方案1】:

    你在寻找这样的东西吗?

    Process proc = new Process();
    proc.StartInfo.FileName = @"C:\WINNT\notepad.exe";
    proc.StartInfo.UseShellExecute = false;
    proc.StartInfo.CreateNoWindow = true;
    
    proc.StartInfo.Domain = "mydomain.com"; // Domain of IIS Computer
    proc.StartInfo.UserName = "kaung"; //Administrator for that computer
    System.Security.SecureString password = new System.Security.SecureString();
    password.AppendChar('m'); //Password
    password.AppendChar('y');
    password.AppendChar('p');
    password.AppendChar('a');
    password.AppendChar('s');
    password.AppendChar('s');
    password.AppendChar('w');
    password.AppendChar('o');
    proc.StartInfo.Password = password;
    
    proc.Start();
    

    【讨论】:

      【解决方案2】:

      问题是我需要重定向所有 3 个流;我只是重定向 2(out,err,not in)。这基本上解决了问题。

      【讨论】:

        猜你喜欢
        • 2020-06-08
        • 2019-11-13
        • 1970-01-01
        • 1970-01-01
        • 2014-05-30
        • 2018-03-25
        • 1970-01-01
        • 2019-01-05
        • 2018-05-23
        相关资源
        最近更新 更多