从ASP.NET应用程序如何启动/停止Windows服务 - 安全问题 [英] How to Start/Stop a Windows Service from an ASP.NET app - Security issues
问题描述
下面是我的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:
- 的问题是安全相关的,因为发生在NetworkService帐户没有足够的权限来启动/停止服务
- 我创建一个本地用户帐户,并将其添加到超级用户组(本组几乎管理员权限)
- 我不希望我的整个Web应用程序,以模拟该用户所有的时间,所以我冒充只在我操作的服务的方法。我用以下资源来帮我做在code做到这一点:
注意:我不通过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屋!