Failing to run C# process as different user

2

I'm trying to run djoin.exe tool with System.Diagnostics.Process from a C# service using a different user (not as the service user).

The process returns code -1073741502.

In the event log I can see:

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

No stderr or stdout.

Here is the process configurations I used:

ProcessStartInfo startInfo = new ProcessStartInfo
{
    Arguments = "/Provision /Domain domain.com /Machine PC12 /SaveFile NUL /printblob",
    WorkingDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
    FileName = "djoin.exe"
    UseShellExecute = false,
    RedirectStandardError = true,
    RedirectStandardInput = true,
    RedirectStandardOutput = true,
    CreateNoWindow =true,
    Domain = "domain.com",
    UserName = "other-user",
    Password = "***"
};

if (username!=null)
{
    startInfo.Domain = domain;
    startInfo.UserName = username;
    startInfo.Password = ToSecureString(password);
}

p = new Process { StartInfo = startInfo };
p.Start();

When using the RUNAS command, everything works fine.

What is the problem?

c#
.net
impersonation
asked on Stack Overflow Sep 28, 2014 by Igal • edited Sep 28, 2014 by marc_s

1 Answer

0

Seems like it is a permissions issue. This can either be at the folder level of where the exe is located, or to do with the user that the process is running under.

To diagnose this, you can first go to the folder where the exe is located. Then right click and set the permissions to "everyone" with full control. Then try to run again and see if you get the same message.

Also when you run Visual studio, at the start, right click and run as administrator. I take it from your comment that this works OK, leading me to believe it is in fact permission related. e.g. Are the different users in the same domain? Once you work out the permissions of the folder where the applcation lives, create an account with permission on that folder and then have whatever process schedules/runs the exe to execute under that account.

Update - the comments above prompted another idea, you could use system.diagnostics to write eventlog entries at each point of the code, to help determine what is going wrong. Another tool that may be of use if WinDBG to get more info about what is throwing that exception.

answered on Stack Overflow Sep 28, 2014 by Robert Anderson • edited Sep 29, 2014 by Robert Anderson

User contributions licensed under CC BY-SA 3.0