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

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David Johnson, CDRetiredCommented:
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-
kaufmed   ( ⚆ _ ⚆ )Commented:
Why are you doubling-up the backslashes in your filename? This isn't C#   ; )
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

DFPITCAuthor Commented:
i'm not trying to run a UI from, 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.;EN-US;165194

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.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DFPITCAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.