从ASP.NET应用程序如何启动/停止Windows服务 - 安全问题 [英] How to Start/Stop a Windows Service from an ASP.NET app - Security issues

查看:179
本文介绍了从ASP.NET应用程序如何启动/停止Windows服务 - 安全问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Here's my Windows/.NET security stack:


  • Windows服务在Windows Server 2003的盒子作为本地系统运行。

  • 的.NET 3.5网站在同一机器上运行,在默认生产服务器IIS设置(这样可能是因为NETWORKSERVICE用户?)

在我的默认VS2008开发环境我有这样的一种方法,这将会从ASP.NET应用程序,它工作正常叫做:

On my default VS2008 DEV environment I have this one method, which gets called from the ASP.NET app, which works fine:

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的以下错误:

When I run this on the production server, I get the following error from the ServiceController:

来源:System.ServiceProcess - >
  System.ServiceProcess.ServiceController - >的IntPtr
  GetServiceHandle(Int32)已 - > System.InvalidOperationException消息:
  无法打开计算机。eTimeSheetReminderService服务。

Source: System.ServiceProcess -> System.ServiceProcess.ServiceController -> IntPtr GetServiceHandle(Int32) -> System.InvalidOperationException Message: Cannot open eTimeSheetReminderService service on computer '.'.

这究竟是为什么,以及如何解决这个问题?

编辑:

答案是以下,大多在评论,但澄清:

The answer is below, mostly in comments, but to clarify:


  1. 的问题是安全相关的,因为发生在NetworkService帐户没有足够的权限来启动/停止服务

  2. 我创建一个本地用户帐户,并将其添加到超级用户组(本组几乎管理员权限)

  3. 我不希望我的整个Web应用程序,以模拟该用户所有的时间,所以我冒充只在我操作的服务的方法。我用以下资源来帮我做在code做到这一点:

MS KB文章并的此,只是为了更好地理解

注意:我不通过web.config中冒充,我做在code。见上面的MS知识库文章。

NOTE: I don't impersonate via the web.config, I do it in code. See the MS KB Article above.

推荐答案

尝试添加这对你的web.config。

Try adding this to your Web.Config.

<identity impersonate="true"/>

这篇关于从ASP.NET应用程序如何启动/停止Windows服务 - 安全问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆