Geeks With Blogs
Omar AL Zabir[MVP] My life with .NET

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

}

 

Posted on Tuesday, October 18, 2005 3:18 PM .NET General | Back to top


Comments on this post: .NET 2.0 Process Impersonation feature not working in Windows App

# re: .NET 2.0 Process Impersonation feature not working in Windows App
Requesting Gravatar...
I got the same problem..did you manage to make it work?
Left by rod on May 19, 2006 12:43 PM

# re: .NET 2.0 Process Impersonation feature not working in Windows App
Requesting Gravatar...
Nope, no solution found yet.
Left by Omar AL Zabir on May 21, 2006 8:56 PM

# re: .NET 2.0 Process Impersonation feature not working in Windows App
Requesting Gravatar...
I'm having the same problem and I haven't been abel to find anything on this also.

Any luck?
Left by Luis Bonilla on Oct 06, 2006 1:21 AM

# re: .NET 2.0 Process Impersonation feature not working in Windows App
Requesting Gravatar...
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))
Left by Efren on Dec 15, 2006 1:07 AM

Your comment:
 (will show your gravatar)


Copyright © Omar AL Zabir [MVP] | Powered by: GeeksWithBlogs.net