Welcome to MSDN Blogs Sign in | Join | Help

Running Processes as a Different User

Before Whidbey, if you wanted to run code as a different user, you needed to use impersonation.  There was no easy solution for starting a new process and having it run with a different user's credentaials.  Probably the best solution in v1.0 and 1.1 of the framework was to P/Invoke out to CreateProcessWithLogonW, which required creating a P/Invoke signature and dealing with unmanaged interop.

The Process class in Whidbey provides a mechanism which allows you to specify the user context that the new process should run under.  This is exposed through three new properties on the ProcessStartInfo class, Domain, UserName, and Password.  UserName and Domain are exactly what you would expect, strings representing the user to log on, and the domain that the user is a member of.

Creating a process as a different user is also one of the first uses of the new SecureString feature, since the Password property is a SecureString.  In order for this to work, you need to make sure that you're not using ShellExecute by setting the UseShellExecute property of the ProcessStartInfo object to false.

Here's some sample code that acts as a very basic RunAs command.  The GetPassword function can be found in my posting about SecureString.

Console.Write("Username: ");
string user = Console.ReadLine();
string[] userParts = user.Split('\\');
        
Console.Write("Password: ");
SecureString password = GetPassword();

try
{
    ProcessStartInfo psi = new ProcessStartInfo(args[0]);
    psi.UseShellExecute = false;
            
    if(userParts.Length == 2)
    {
        psi.Domain = userParts[0];
        psi.UserName = userParts[1];
    }
    else
    {
        psi.UserName = userParts[0];
    }

    psi.Password = password;

    Process.Start(psi);
}
catch(Win32Exception e)
{
    Console.WriteLine("Error starting application");
    Console.WriteLine(e.Message);
}

Published Wednesday, June 02, 2004 11:17 AM by shawnfa
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Running Processes as a Different User

Wednesday, June 02, 2004 2:30 PM by dominick
great stuff!

# Running processes

Wednesday, June 02, 2004 5:57 PM by Alex's ASP.NET Blog

# Secure Coding: Running Processes as a Different User

Wednesday, June 02, 2004 7:07 PM by Dana Epp's ramblings at the Sanctuary
Shawn has posted an interesting entry about how in Whidbey you can use the Process class to specify the user context that the new process should run under. This differs significantly from current approaches, as you normally have to P/Invoke CreateProcessWithLogonW to do it through impersonation. I've talked about different approaches before when I discussed spawning external processes securely in Windows and using restricted tokens to execute a process, but this is much more elegant. It's nice to see the Process class add new functionality through the exposure of three new properties on the ProcessStartInfo class: Domain, UserName, and Password. Here is a snippit that Shawn used (although of course you would do better input validation than that :) ): Console.Write("Username: ");string user = Console.ReadLine();string[] userParts = user.Split('\\');        Console.Write("Password: ");SecureString password = GetPassword();try{    ProcessStartInfo psi = new ProcessStartInfo(args[0]);    psi.UseShellExecute = false;                if(userParts.Length == 2)    {        psi.Domain = userParts[0];        psi.UserName = userParts[1];    }    else    {        psi.UserName = userParts[0];    }    psi.Password = password;    Process.Start(psi);}catch(Win32Exception e){    Console.WriteLine("Error starting application");    Console.WriteLine(e.Message);} Anyways, nice find Shawn!...

# RE: How can I run another application or batch file from my Visual C# .NET code?

Wednesday, June 02, 2004 8:59 PM by C# Frequently Asked Questions

# re: Running Processes as a Different User

Wednesday, June 02, 2004 9:30 PM by Ryan Farley
Shawn,

That's some cool info - glad to see that is coming. As a matter of fact, I just got through writing some code today that calls to CreateProcessWithLogonW (and came accross your comment from a post on C# FAQ)! Weird how interconnected things become in the blogsphere.

-Ryan

# Ejecuci

Friday, June 04, 2004 2:55 PM by JASoft.org
La versi

# re: Making Strings More Secure

Monday, June 07, 2004 3:30 PM by .Net Security Blog

# re: Running Processes as a Different User

Friday, July 30, 2004 8:20 AM by Chris Staley
Yes, thank you for correcting (what I feel was) an obvious omission from the Process class.

# CreateProcessWithLogonW

Tuesday, August 31, 2004 6:38 PM by K. Scott Allen's Blog

# Process.Start and Impersonation

Thursday, November 18, 2004 6:09 PM by Coding Horror
Did you know that Process.Start always uses the security context of the parent ASP.NET process? I just found this out the hard way; Using Process.Start on "whoami.exe" always returns the ASPNET worker process no matter what I do. Some...

# I learned that there&#039;s alot of mature people out there who can talk about sex in an intellectual manner and like "easy" sites like <a href=&#039;http://www.russian-bitches.info&#039;>http://www.russian-bitches.info</a>

Thursday, March 09, 2006 9:59 AM by Duncan@netmigommy.net
Valery

# This is something i&#039;m looking for an honest opinion. Have you done or been curious about DP (double penetration)? Is it something that women wonder about just out of curiousity or if you women have done it, is it as great as it claims to be, like

Thursday, March 09, 2006 10:02 AM by shannon_n@merelink-mono.com
Nadja

# Very interesting and beautiful site. It is a lot of helpful information. Thanks!

Sunday, March 12, 2006 10:14 AM by napavalleysfsoft3006@yahoo.com
Martin

# Running Processes with parameter

Saturday, November 11, 2006 8:11 AM by houshyar saboktakin

hello,

How I can run my batch file with parameters

in c#?

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker