6

I run an external program from ASP.NET:

var process = new Process();
var startInfo = process.StartInfo;

startInfo.FileName = filePath;
startInfo.Arguments = arguments;

startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
//startInfo.RedirectStandardError = true;

process.Start();

process.WaitForExit();

Console.Write("Output: {0}", process.StandardOutput.ReadToEnd());
//Console.Write("Error Output: {0}", process.StandardError.ReadToEnd());

Everything works fine with this code: the external program is executed and process.StandardOutput.ReadToEnd() returns the correct output.

But after I add these two lines before process.Start() (to run the program in the context of another user account):

startInfo.UserName = userName;
startInfo.Password = securePassword;

The program is not executed and process.StandardOutput.ReadToEnd() returns an empty string. No exceptions are thrown.

userName and securePassword are correct (in case of incorrect credentials an exception is thrown).

How to run the program in the context of another user account?

Environment: .NET 4, Windows Server 2008 32bit

UPD:

The application works fine under ASP.NET development server + Windows 7, but fails on IIS 7 + Windows Server 2008 Web Edition.

UPD2:

Found this in the event log:

Faulting application cryptcp.exe, version 3.33.0.0, time stamp 0x4be18460, faulting module kernel32.dll, version 6.0.6002.18005, time stamp 0x49e03821, exception code 0xc0000142, fault offset 0x00009eed, process id 0xbf4, application start time 0x01caf1b91f5b851a.

cryptcp.exe is the name of external application.

alexey
  • 8,360
  • 14
  • 70
  • 102
  • On the other hand, this question http://stackoverflow.com/questions/2345620/alternative-to-allow-service-to-interact-with-desktop, found by searching for that same fault offset and kernel32.dll might indicate that it's an unsurmountable problem once you're running under IIS - the cryptcp.exe must want to interact with the desktop in some way (by my reading) – Damien_The_Unbeliever May 13 '10 at 12:23

4 Answers4

3

I realize this was asked a while ago but I encountered the same problem and found the solution on this website: The Perils and Pitfalls of Launching a Process Under New Credentials

Applying the solution under section Application Failed to Initialize Properly fixed it for me.

Hope this will save some others time and frustration!

COD3BOY
  • 11,964
  • 1
  • 38
  • 56
Koen
  • 31
  • 1
2

According to Microsoft, you can't read standard output AND standard error like that as it winds up deadlocking. To resolve, use something like the following:

private readonly StringBuilder outputText = new StringBuilder();
private readonly StringBuilder errorText = new StringBuilder();

. . .

        process.OutputDataReceived += delegate(
            object sendingProcess,
            DataReceivedEventArgs outLine)
        {
            if (!string.IsNullOrEmpty(outLine.Data))
            {
                outputText.AppendLine(outLine.Data);
            }
        };

        process.ErrorDataReceived += delegate(
            object sendingProcess,
            DataReceivedEventArgs errorLine)
        {
            if (!string.IsNullOrEmpty(errorLine.Data))
            {
                errorText.AppendLine(errorLine.Data);
            }
        };

        process.BeginOutputReadLine();
        process.BeginErrorReadLine();
        process.WaitForExit();
        Console.WriteLine(errorText.ToString());
        Console.WriteLine(outputText.ToString());
Jesse C. Slicer
  • 19,901
  • 3
  • 68
  • 87
  • Thanks, good comment. But this question addresses another problem. I'll comment error output redirection. – alexey May 13 '10 at 12:00
1

Looking at the MSDN documentation there are a few other items that are recommended to be configured to properly start an application as another user.

  1. Set the domain, username, and password properties (You should set the domain)
  2. Set the working directory as by default with a username/password it is the system32 folder

That might help you get this resolved.

Mitchel Sellers
  • 62,228
  • 14
  • 110
  • 173
1

It may be that the application you're starting needs it's profile loaded (by default, it won't be). Have you tried setting the LoadUserProfile property to true?

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448