First off, I'm not trying to interact with the desktop in any kind.
What I'm trying to do is to separate concerns. I have a service that runs under the local system account (LSA). Sometimes, I need to execute processes under a different account.
I am using System.Diagnostics.Process
for this. This is what I have tried with so far:
The case that does not work for me throws me a Win32Exception with "Access denied." I have granted the user account in question the right to log on as a service, but it does not make any difference.
For fun, I have also tried these scenarios:
Process.Exited
is fired immediately.This is the code I'm working with:
var pi = new ProcessStartInfo {
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = false,
ErrorDialog = false,
RedirectStandardError = false,
RedirectStandardInput = false,
RedirectStandardOutput = false,
FileName = @"C:\Path\To\SomeApplication.exe",
Arguments = @"Some arguments",
UserName = "SomeUserName",
Domain = "SomeDomain",
Password = SecureStringUtils.Convert("SomePassword")
};
var process = new Process();
process.StartInfo = pi;
process.Start(); // Throws when run as a service
Any ideas?
I finally found this: Using Process.Start() to start a process as a different user from within a Windows Service, and it solves my problem. Granted, the code provided in the answer needs a ton of polishing, but it does seem to fix my problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With