【问题标题】:c# run console application as different user... impersonation [duplicate]c#以不同的用户身份运行控制台应用程序...模拟[重复]
【发布时间】:2012-09-12 10:17:03
【问题描述】:
可能重复:
Process.Start() impersonation problem
我正在尝试在 c# 中以其他用户身份运行控制台应用程序,但我遇到了问题。这是我的代码的一部分:
public void encryptPGP(string fileName)
{
string sCommandLine = "" +
"--recipient \"User Name <username@domain.com>\" --encrypt \"" + fileName + "\"";
System.Diagnostics.Process.Start("C:\\Utilities\\GnuPG\\App\\gpg.exe", sCommandLine);
}
... 我需要让某个用户运行 C:\Utilities\GnuPG\App\gpg.exe。我将如何添加它?
这将在 Web 应用程序中使用。
谢谢!
【问题讨论】:
标签:
c#
.net
web-services
web-applications
impersonation
【解决方案1】:
您可以使用System.Diagnostics.ProcessStartInfo 类来提供您希望在其下启动进程的用户凭据。请参阅下面的示例代码:
string userPwd = "secretPassword";
System.Security.SecureString pwd = new System.Security.SecureString();
foreach (char c in userPwd.ToCharArray())
{
pwd.AppendChar(c);
}
System.Diagnostics.ProcessStartInfo inf = new System.Diagnostics.ProcessStartInfo();
inf.FileName="path to file";
inf.Domain = "domainname";
inf.UserName = "desired_user_name";
inf.Password = pwd;
System.Diagnostics.Process.Start(inf);
我个人还没有测试过这个解决方案,但它应该可以在 framework 2.0 之后工作。
希望对你有帮助。