Omar AL Zabir[MVP]

My life with .NET
posts - 30, comments - 0, trackbacks - 43

My Links

News

Archives

Post Categories

Image Galleries

.NET

Articles

Tools

.NET 2.0 Process Impersonation feature not working in Windows App

.NET 2.0 introduced an interesting feature in the "Process" class where you can specify UserName, Password and it runs the process as the specified user. However, it only works inside Console Application. It does not run if the application is a Windows Application. It throws "Win32Exception Access Denied" when the Start() is called. Is it as designed? I really need this feature in a windows service.

Try the following code in a console app, it will run perfectly. But change the application type to "Windows Application" and run, it will throw exception. IF you do not set the UserName, Password, it will run then. I am using VS 2005 Release Candidate.

Here's the code:

ProcessStartInfo startInfo = new

ProcessStartInfo(@"SampleEXE.exe");

startInfo.Domain = ".";

startInfo.UserName = "test";

// create a secure string from the password we have

SecureString securePassword = new SecureString();

foreach (char c in "test")

securePassword.AppendChar(c);

securePassword.MakeReadOnly();

startInfo.Password = securePassword;

startInfo.UseShellExecute = false;

startInfo.RedirectStandardOutput = true;

startInfo.RedirectStandardError = true;

startInfo.RedirectStandardInput = false;

startInfo.CreateNoWindow = true;

startInfo.WindowStyle = ProcessWindowStyle.Hidden;

startInfo.WorkingDirectory =

Path.GetDirectoryName(startInfo.FileName);

//if( info.Properties.ContainsKey(ARGUMENTS_ATTRIBUTE) )

// startInfo.Arguments =

info.Properties[ARGUMENTS_ATTRIBUTE];

startInfo.Arguments = "Hi Hello How are you?";

using (Process p = new Process())

{

p.StartInfo = startInfo;

p.Start();

string fileName = @"output.txt";

using (StreamWriter writer = new StreamWriter(fileName))

{

writer.Write(p.StandardOutput.ReadToEnd());

writer.Close();

}

p.WaitForExit();

p.Close();

}

 

Print | posted on Tuesday, October 18, 2005 3:18 PM | Filed Under [ .NET General ]

Feedback

Gravatar

# re: .NET 2.0 Process Impersonation feature not working in Windows App

I got the same problem..did you manage to make it work?
5/19/2006 12:43 PM | rod
Gravatar

# re: .NET 2.0 Process Impersonation feature not working in Windows App

Nope, no solution found yet.
5/21/2006 8:56 PM | Omar AL Zabir
Gravatar

# re: .NET 2.0 Process Impersonation feature not working in Windows App

I'm having the same problem and I haven't been abel to find anything on this also.

Any luck?
10/6/2006 1:21 AM | Luis Bonilla
Gravatar

# re: .NET 2.0 Process Impersonation feature not working in Windows App

Does anyone know if this works on a web app? I have a C# app and this approach does not seem to work (always using ASPNET or NETWORK SERVICE, (iis5.1/iis6))
12/15/2006 1:07 AM | Efren
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 

Powered by: