none
Invoking process under different user from ASP.NET web service RRS feed

  • Question

  • Is there a way to run process as different user from ASP.NET rest API web application which runs as service.

    If impersonation used before calling System.Diagnostic.Proccess.Start() it does not execute it under impersonated user.

    However, if explicitly set username, password into ProcessInfo property that still does not execute – Access Denied:

    if (!string.IsNullOrEmpty(userName))

                    {

                        process.StartInfo.UserName = userName;

                        Console.WriteLine(userName);

                        if (!string.IsNullOrEmpty(domain))

                        {

                            process.StartInfo.Domain = domain;

                            Console.WriteLine(domain);

                        }

                    }

                    if (!string.IsNullOrEmpty(password))

                    {

                        process.StartInfo.PasswordInClearText = password;

                    }

                    process.StartInfo.LoadUserProfile = false;

                    process.StartInfo.WorkingDirectory = @"C:\working";

                    process.StartInfo.CreateNoWindow = true;

                    process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

                    process.StartInfo.UseShellExecute = false;

                    process.StartInfo.RedirectStandardError = true;

                    process.StartInfo.RedirectStandardOutput = true;

                    process.StartInfo.LoadUserProfile = false;

                    var stdOutput = new StringBuilder();

                    process.OutputDataReceived += (sender, args) => stdOutput.Append(args.Data);

                    string stdError = null;

                    try

                    {

                        cmdExecmodle.OutputResult = " " + RuntimeInformation.OSDescription + "\r\n";

                        cmdExecmodle.OutputResult += " " + RuntimeInformation.ProcessArchitecture.ToString() + "\r\n";

                        cmdExecmodle.OutputResult += " " + WindowsIdentity.GetCurrent().Name + "\r\n";

                        process.Start();

                         process.BeginOutputReadLine();

                      

                        if (waitForExit)

                        {

                            process.WaitForExit();

                        }

                        else

                        {

                            Thread.Sleep(500);

                        }

                    }

                    catch (Exception e)

                    {

                        throw new Exception("OS error while executing " + Format(filename, arguments) + ": " + e.Message, e);

                    }

    If this is run from debug and/or IISExpress it works properly.

    I've tried running service both under Local System and Administrator too. Also, I've been giving additional permissions to Administrator like:

    • Log on as a service Act as part of the operating system Adjust memory quotas for a process Replace a process level token

    which did not helped.

    Any ideas?

    Thursday, May 28, 2020 8:44 AM

All replies

  • Hi ljubop,

    Since this thread is related to ASP.NET, so I suggest that you can ask this question in the ASP.NET forum for better support.

    The Visual C# forum discusses and asks questions about the C# programming language, IDE, libraries, samples, and tools.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Thursday, June 4, 2020 1:46 AM
  • I got a similar issue a couple of weeks ago but I tried asking in ASP.NET forum. They really help me with my problem in few days. You can try going there
    Saturday, June 6, 2020 7:32 PM