System.Diagnostics.Process 不接受凭据

发布于 2024-12-13 04:55:19 字数 638 浏览 6 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

平生欢 2024-12-20 04:55:19

问题是我需要重定向所有 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.

心病无药医 2024-12-20 04:55:19

您在寻找这样的东西吗?

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();

Are you looking for something like this?

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文