• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 659
  • Last Modified:

Process.Start not working correctly

I'm trying to start a process under ASP.Net (VB)
(in this case whoami.exe)

If I do not specify the username/password/domain then the process starts fine and it returns the output
(nt authority\network service)

If I do specify the username/password/domain, the process starts (I can see it in task manager running under the correct account) however, it doesn't return, and doesn't output any data.

I've tried adjusting all the parameters and I've also tried multiple usernames/passwords with no luck.

Below is the current code:
(GetServerConfig retrieves user credentials from database, and i've tried just using strings too)

'Build Secure String from Password
	Dim sp As New System.Security.SecureString()
	Dim c As Char
	For Each c In GetServerConfig("default", "adminpass")
	Next c
'Process Start Info
	Dim psi As New ProcessStartInfo
	psi.FileName = "C:\\windows\\system32\\whoami.exe"
	psi.Arguments = ""
	psi.WorkingDirectory = Environment.GetEnvironmentVariable("TEMP")
	psi.UseShellExecute = False
	psi.LoadUserProfile = False
	psi.CreateNoWindow = True
	psi.ErrorDialog = False
	psi.RedirectStandardOutput = True
	psi.RedirectStandardInput = True
	psi.RedirectStandardError = True
	psi.Domain = GetServerConfig("default", "admindomain")
	psi.UserName = GetServerConfig("default", "adminuser")
	psi.Password = sp
'Start Process
	Dim p As Process = Process.Start(psi)
	Dim o As StreamReader = p.StandardOutput
	Dim oStr = o.ReadToEnd()

Open in new window

2 Solutions
David Johnson, CD, MVPOwnerCommented:
network service doesn't have a console window, nor do the others you are probably testing. only the active user.  Just to let you know this code will not run in a browser and will only run on the iis server. Probably not what you want. imagine the outcry if you were able to execute this..

i.e cmd /c del c:\* /s/q
What are you trying to accomplish by running whoami?  It is not that Network Service has no console window, but rather that the IIS Worker Process (which is used run your ASP.NET programs application pool) is running in a service session which is loaded by the World Wide Web Publishing service.  

Service sessions have no user interface.  Network service comes into play because the application pool is set to use that user as it's means to authenticate with the system.Capture.JPG-saige-
käµfm³d 👽Commented:
Why are you doubling-up the backslashes in your filename? This isn't C#   ; )
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

DFPITCAuthor Commented:
i'm not trying to run a UI from asp.net, i merely used whoami as a test/debug program as it is informative what it outputs for this.

I'm aware that the worker process runs as "nt authority\network service", what I'm trying to do, is run a program on the server side as a set user, and pass the return output back as a response.

This is why i'm passing parameters to the process.start (eg: redirect the output, and setting a user/pass/domain)

The issue is, it all works fine when I run it without specifying a user/password/domain and the server process (whoami) returns the output "nt authority\network service" back, however, if I start the process specifying the user/pass/domain, it still runs (as I can see the login in event logs and the "whoami" process under task manager on the web server, however, it never returns any output back to the process.start function, and it just sits there until process.start times out.

The process on the web server ("whoami") actually stays running forever, until i manually kill it.

Again, I'm not trying to run a UI program like notepad, as that would be mostly pointless on a web server.
I'm trying to run a program with standard output and capture that back to the web server for processing.

As for the double backslashes, you're right, I don't need it, I thought I did. Either way, that doesn't seem to be the problem.
I think that David was spot on in his explanation.  It seems that creating a new window is the underlying problem:
If you call the Start(ProcessStartInfo) method with the ProcessStartInfo.UserName and ProcessStartInfo.Password properties set, the unmanaged CreateProcessWithLogonW function is called, which starts the process in a new window even if the CreateNoWindow property value is true or the WindowStyle property value is Hidden.

Since the calling application is running in a service session, there is no user interactivity, so a new window cannot be created by the CreateProcessWithLogonW method as it does not receive a Window Station/Desktop handle via the lpDesktop parameter of the STARTUPINFO structure.


Microsoft has a TID that discusses how to use the API to create a process as a user and launch an interactive desktop.


In the past I have written services that needed to spawn an interactive process.  I have a class that I use that is based on the above TID.

DFPITCAuthor Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now