Search code examples
asp.netiiswindows-servicesaccess-deniedservicecontroller

Access is denied when start/stop Windows Service with ASP


I wrote an ASP.NET Web Application that shows all installed services for our product. My application can retrieve the state of service (running, stopped,...).

Additionally, my application should be able to start a stopped service and stop a running service. On my local machine it works, on the server it doesn't. Currently I configured the web-app to use Administrator, but it doesn't start. The Event Viewer shows, that the access is denied:

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: 

Any idea what I missed?

Just as additional info, here my StartService-funtion. But should not be a problem, since it works on my local machine:

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;
}

Solution

  • iis user or iis application pool does not have enough privilege to start or stop the windows service. so you need to add iusr, iis_iusrs or APPPOOL\ServiceManager to the admin group so that you can access windows service. this way is easy, but the unrecommended way. another way you could use account who has enough privilege to access the windows service and set in iis application pool custom account. you can find this setting under the application pool advance setting.

    enter image description here