简体   繁体   English

LocalSystem Service如何以用户[C#]身份运行应用程序?

[英]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.

相关问题 如何在本地系统帐户下禁止C#服务启动? - How to forbid C# service from start under LocalSystem account? 如何在C#中创建可以与GUI *或*一起运行的Windows应用程序? - How can I create a Windows application that can run with a GUI *or* as a Windows service in C#? 如何通过 c# 中的服务应用程序运行 GUI 应用程序 - How run an GUI application via a Service application in c# c# - 如何使应用程序作为服务运行? - c# - how do i make application run as a service? 如何暂停C#控制台应用程序(仅在由用户运行时) - How can I pause a C# Console application (only when being run by a user) 如何通过服务帐户和集成安全性运行WPF(C#)应用程序 - How to run wpf(c#) application by service account and integrated security 使用LocalSystem Service CreateProcessAsUser启动程序相当于登录时双击该图标? [C#] - Launching a program using LocalSystem Service CreateProcessAsUser equivalent to double-clicking on the icon when logged in? [C#] C#-使用Visual Studio安装项目安装时,在LocalSystem下运行的Windows Service提示输入凭据 - C# - Windows Service running under LocalSystem is prompting for credentials when installing using Visual Studio setup project 用户可配置的C#Windows服务应用程序 - User Configurable C# Windows Service Application 在 LocalSystem C# 下运行的 RunAs - RunAs running under LocalSystem C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM