如何从ASP.NET应用程序启动/停止Windows服务 – 安全问题

这是我的 Windows / .NET安全堆栈:

> Windows Server 2003上运行作为LocalSystem的Windows服务.
> .NET 3.5网站运行在同一个框中,在“认”生产服务器IIS设置下(所以可能是NETWORKSERVICE用户?)

在我的认VS2008 DEV环境我有这个方法,它从ASP.NET应用程序调用,它工作正常:

private static void StopStartReminderService() {

    ServiceController svcController = new ServiceController("eTimeSheetReminderService");

    if (svcController != null) {
        try {
            svcController.Stop();
            svcController.WaitForStatus(ServiceControllerStatus.Stopped,TimeSpan.FromSeconds(10));
            svcController.Start();
        } catch (Exception ex) {
            General.ErrorHandling.LogError(ex);
        }
    }
}

当我在生产服务器上运行它时,我从ServiceController得到以下错误

Source: System.ServiceProcess ->
System.ServiceProcess.ServiceController -> IntPtr
GetServiceHandle(Int32) -> system.invalidOperationException Message:
Cannot open eTimeSheetReminderService service on computer ‘.’.

为什么会发生这种情况,我该如何解决

编辑:

答案在下面,主要是在评论中,但要澄清:

>问题是与安全相关的,并发生,因为NETWORKSERVICE帐户没有足够的权限启动/停止服务
>我创建了一个本地用户帐户,并将其添加到PowerUsers Group(该组拥有几乎管理员权限)
>我不想让我的整个Web应用程序一直模仿这个用户,所以我只在我操纵服务的方法中模拟.我通过使用以下资源来帮助我在代码中执行此操作:

MS KB articlethis,just to get a better understanding

注意:我不是通过web.config模拟,我在代码中做.请参阅上面的MS KB文章.

解决方法

尝试将其添加到您的Web.Config.
<identity impersonate="true"/>

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....