System.Diagnostics.Process 不接受凭据
我正在尝试使用 Process 类执行批处理文件。此代码位于较大代码部分的中间,我在其中使用 LogonUser() 和 WindowsIdentity.Impersonate() 来模拟本地 PC 管理员帐户。
我试图在进程中运行批处理文件,而不在 ProcessStartInfo 中添加凭据,但这样做会导致批处理文件无提示地失败 - 没有引发错误,并且从未返回批处理文件的预期输出(我正在阅读stderr 和 stdout 异步,fwiw)。
然后,我将凭据添加到 ProcessStartInfo,但现在如果我不首先调用 WindowsImpersonationContext.Undo(),则会收到“访问被拒绝”错误;如果我确实调用 .Undo,则会收到“登录失败:未知的用户名或错误密码”错误() 在 Process.Start() 之前。我已经三次检查多个帐户的用户名/密码/域是否正确。
如果我的代码没有 LogonUser() 或 WindowsIdentity.Impersonate() 调用(并且 ProcessStartInfo 中没有凭据),那么我在执行批处理文件和捕获批处理文件的输出时没有问题。
我能够以本地管理员或任意本地用户帐户的身份从桌面成功运行批处理文件。我可以看到它的权限表明它应该可以从我尝试运行它的帐户中读取/执行。这实在是令人困惑。任何帮助表示赞赏。
I am trying to execute a batch file using the Process class. This code is in the middle of a larger section of code where I am using LogonUser() and WindowsIdentity.Impersonate() to impersonate the local PC admin account.
I was attempting to run a batch file within a Process, without adding credentials in ProcessStartInfo, but doing it that way caused the batch file to fail silently - no errors were raised, and expected output from the batch file never was returned (I am reading stderr and stdout asynchronously, fwiw).
I then added the credentials to ProcessStartInfo, but now I get an "Access is denied" error if I do not first call WindowsImpersonationContext.Undo(), and an "Logon failure: unknown username or bad password" error if I do call .Undo() before Process.Start(). I have triple-checked that the username/password/domain is correct, for multiple accounts.
If my code has no LogonUser() or WindowsIdentity.Impersonate() calls (and no credentials in ProcessStartInfo), then I don't have a problem with the batch file executing and output from batch file being captured.
I am able to run the batch file from the desktop successfully, either as the local admin or an arbitrary local user account. I can see its permissions show that it should be readable/executable from the accounts I am trying to run it. This is really quite the stumper; any help is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是我需要重定向所有 3 个流;我只重定向了 2(出,错误,不入)。这基本上解决了问题。
The problem was that I needed to redirect all 3 streams; I was only redirecting 2 (out, err, not in). That basically fixed things.
您在寻找这样的东西吗?
Are you looking for something like this?