asp.net - 使用 ASP 启动/停止 Windows 服务时访问被拒绝

标签 asp.net iis windows-services access-denied servicecontroller

我编写了一个 ASP.NET Web 应用程序,它显示我们产品的所有已安装服务。我的应用程序可以检索服务状态(正在运行、已停止......)。

此外,我的应用程序应该能够启动已停止的服务并停止正在运行的服务。在我的本地计算机上它可以工作,在服务器上则不能。目前我将网络应用程序配置为使用管理员,但它无法启动。 事件查看器显示访问被拒绝:

Process information: 
    Process ID: 5348 
    Process name: w3wp.exe 
    Account name: IIS APPPOOL\ServiceManager 

Exception information: 
    Exception type: Win32Exception 
    Exception message: Access is denied



Request information: 
    Request URL: http://X/ServiceMonitor/StopService/25 
    Request path: /ServiceMonitor/StopService/25 
    User host address: X 
    User:  
    Is authenticated: False 
    Authentication Type:  
    Thread account name: X\Administrator 

Thread information: 
    Thread ID: 23 
    Thread account name: X\Administrator 
    Is impersonating: False 
    Stack trace: 

知道我错过了什么吗?

作为附加信息,这里是我的 StartService 函数。但这应该不是问题,因为它可以在我的本地计算机上运行:

public bool StartService()
{
    ServiceController service = new ServiceController(_serviceName,_machineName);
    if (!new[] { ServiceControllerStatus.Running, ServiceControllerStatus.StartPending }.Contains(service.Status))
        service.Start();

    service.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromMinutes(2));

    return service.Status == ServiceControllerStatus.Running;
}

最佳答案

iis 用户或 iis 应用程序池没有足够的权限来启动或停止 Windows 服务。所以你需要将 iusr、iis_iusrs 或 APPPOOL\ServiceManager 添加到 admin 组中,以便可以访问 windows 服务。这种方式很简单,但不推荐。另一种方法是,您可以使用有足够权限访问 Windows 服务的帐户,并在 iis 应用程序池自定义帐户中设置。您可以在应用程序池高级设置下找到此设置。

enter image description here

关于asp.net - 使用 ASP 启动/停止 Windows 服务时访问被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58430016/

相关文章:

asp.net - SignalR 在重建后使 IIS 挂起

用于安全 Internet 连接的 WCF 选项

windows-services - PsExec 在非交互式运行时卡在许可证提示符上

javascript - 通过缩短变量名来减少 json 字符串的大小

c# - 使用 ASP.NET 5 和内置 DI 容器的每个请求范围

ASP.net Vb.Net Label.Text 不更新

c# - Windows 服务输出未显示在事件日志中

asp.net - ASP.NET 中的多语言静态内容

iis - Moodle IIS崩溃

c# - 是否可以将 Windows 服务添加到现有项目中?