0

Possible Duplicate:
Process.Start() impersonation problem

I am trying to run a console application as a different user in c#, but I am having trouble. Here is part of my code:

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

... I need to have the C:\Utilities\GnuPG\App\gpg.exe ran by a certain user. How would I add that in?

This will be used in a web application.

Thanks!

Community
  • 1
  • 1
user1663372
  • 11
  • 1
  • 3

1 Answers1

1

You can use the System.Diagnostics.ProcessStartInfo class to provide user credentials under which you want to start the process. See sample code below :

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

I personally have not tested this solution, but it should work framework 2.0 onward.

Hope it helps you.

AYK
  • 3,312
  • 1
  • 17
  • 30