Reputation: 62469
I'm trying to start a child process under the Network Service account and I just can't get the right combination of username/password. Code:
var proc = new Process();
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.FileName = "Test2.exe";
proc.StartInfo.Arguments = args[0];
proc.StartInfo.Domain = "NT AUTHORITY";
proc.StartInfo.UserName = "NETWORK SERVICE";
proc.StartInfo.Password = new SecureString();
proc.Start();
Upvotes: 1
Views: 984
Reputation: 3026
A quick google search revealed this page. It appears you will need to rely on the win32 api, specifically these methods:
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool LogonUser(string username, string domain,
string password, LogonType logonType,
LogonProvider logonProvider,
out IntPtr userToken);
[DllImport("advapi32.dll", SetLastError = true)]
static extern bool ImpersonateLoggedOnUser(IntPtr userToken);
I'm guessing the reason it's not so simple to do this in .Net is because it is a very odd requirement, as this user should be used by Windows Services, and that is not the concern of the executing code.
Upvotes: 2