[英]How a LocalSystem Service can run an application as a user [C#]?
On my local machine I am running an administrative c# service as LocalSystem called Serv.exe which performs various tasks. 在本地计算机上,我正在运行一个名为Serv.exe的管理性c#服务,称为LocalSystem,它执行各种任务。 One of the tasks it needs to perform is to launch an application under the USER account (currently logged on), not as admin - as this violates security. 它需要执行的任务之一是以USER帐户(当前已登录)启动应用程序,而不是以admin身份启动-因为这违反了安全性。
The obvious solution would be simple impersonation when launching the application - however I run into a small problem whereas I am not priviledged to the user account credentials (Username & Password) and therefore am unable to impersonate in the conventional way. 显而易见的解决方案是启动应用程序时的简单模拟-但是我遇到了一个小问题,而我没有特权获得用户帐户凭据(用户名和密码),因此无法以常规方式进行模拟。
So, using a C# service running as LocalSystem when logged on to a User account - is there anyway I can launch an application as that User? 因此,使用登录到用户帐户时作为LocalSystem运行的C#服务-无论如何,我是否可以以该用户身份启动应用程序?
From the comments: 从评论:
what happens is that the Application itself asks the Service to do a job and then terminates. 发生的情况是,应用程序本身要求服务执行一项工作然后终止。 when the job is funished the application must restart itself - I thought the best way would be to have the service restart it when it was done ... 当工作完成后,应用程序必须自行重启-我认为最好的方法是让服务在完成后重启它...
Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks, 谢谢,
You can use Windows Scheduler to start your app as a user. 您可以使用Windows Scheduler以用户身份启动应用程序。
Take a look at this wrapper - http://www.firatatagun.com/c-windows-task-scheduler-wrapper-classes-c-sharp/2010/04/22/ 看看这个包装器-http: //www.firatatagun.com/c-windows-task-scheduler-wrapper-classes-c-sharp/2010/04/22/
and then you can simply create a scheduled task to run your software immediately, afterward you can delete this redundant task after 2 seconds. 然后您只需创建一个计划任务即可立即运行您的软件,然后您可以在2秒后删除此冗余任务。
Sample code: 样例代码:
using (TaskService ts = new TaskService())
{
// Create a new task
const string taskName = "RunMyProcessNowAsUser";
Task t = ts.AddTask(taskName,
new TimeTrigger() { StartBoundary = DateTime.Now, Enabled = false },
new ExecAction("YourProcess.exe");
t.Run();
// delete the task after 2 seconds.
new Action(() =>
{
Thread.Sleep(2000);
using (TaskService ts2 = new TaskService())
{
ts2.RootFolder.DeleteTask(taskName);
}
}).BeginInvoke(null, null);
}
Instead of breaching security this way you can make the application wait and then restart itself. 不必以这种方式破坏安全性,您可以让应用程序等待然后重新启动。 See this SO question and this one . 看到这个问题和这个 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.