C# - Starting process under different user than the IIS application pool user

1
        public static string StartProcess(string cmd, string args, string workingDirectory, string username, string password)
        {
        // launch system process
        ProcessStartInfo startInfo = new ProcessStartInfo(cmd, args);
        startInfo.WindowStyle = ProcessWindowStyle.Hidden;
        startInfo.RedirectStandardOutput = true;
        startInfo.RedirectStandardInput = true;
        startInfo.UseShellExecute = false;
        startInfo.CreateNoWindow = true;
        startInfo.UserName = username;
        startInfo.Password = ConvertStringToSecureString(password);

        // get working directory from executable path
        if (String.IsNullOrEmpty(workingDirectory))
        {
            startInfo.WorkingDirectory = Path.GetDirectoryName(cmd);
        }
        else
        {
            startInfo.WorkingDirectory = workingDirectory;
        }
        var results = new StringBuilder();
        using (var process = Process.Start(startInfo))
        {
            process.StandardInput.Flush();
            process.StandardInput.Close();
            var reader = process.StandardOutput;
            string lineOut;
            while ((lineOut = reader.ReadLine()) != null)
            {
                results.AppendLine(lineOut);
            }
            while (!process.HasExited) System.Threading.Thread.Sleep(1000);
        }
        return results.ToString();
        }

I am trying to launch a process from within my IIS application using a different username / password than the one that the IIS application pool is operating under.

The IIS app pool is running under a full admin user, and when the process starts, it completes, the result is return, no issues. When I specify a username / password, the process starts, then immediately closes.

I also have

Application popup: conhost.exe - Application Error : The application was unable to start correctly (0xc0000142). Click OK to close the application. 

in my event logs. If I take the above code and put into its own exe file and run it via the desktop using the same admin user IIS is running under, the process starts (using the username / password for the other user) and completes without issues.

It appears to just be limited to IIS. Am I missing something?

c#
asp.net
iis
asked on Stack Overflow Dec 8, 2016 by echoDreamz • edited Dec 8, 2016 by echoDreamz

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0