0

I'm using the following code to check my windows service status:

 ServiceController sc = new ServiceController("service name", "service location");

in the service location H'm putting the server name which my service reside on. on my test environment, I have the service and portal (IIS) where I check my service on the same server and it works fine, but on my production environment, The service is on a different server than my portal IIS.

and my code is unable to check the status. I'm sure it is a permission issue, but I tried so many this to make it work but no use.

my question is: what "type of permission" to be given to what "user" or "machine name"? please help.

Ammar Ali
  • 77
  • 1
  • 3
  • 15

2 Answers2

0

Does your IIS portal impersonate? It seems it does. As such you are falling into Kerberos constrained delegation ('two-hop'). You cannot 'fix' this. You have to ask a domain administrator to enable and configure constrained delegation for your IIS application.

An alternative is not to impersonate in your IIS. In this case you need grant appropriate permissions to your portal app pool. The required permission in SC_MANAGER_CONNECT. Read more at KB179249: Access to the Service Control Manager. Obviously the app-pool must be a domain account and the IIS and target service host machine should be in the same domain, or in domains that have a trust relationship.

Remus Rusanu
  • 288,378
  • 40
  • 442
  • 569
0

Thanks to all anyway, I have used another way through WMI and it worked:

ConnectionOptions op = new ConnectionOptions();
op.Username = "";
op.Password = "";
ManagementScope scope = new ManagementScope(SVClocation+@"\root\cimv2", op);
scope.Connect();
ManagementPath path = new ManagementPath("Win32_Service");
ManagementClass services = new ManagementClass(scope, path, null);
foreach (ManagementObject service in services.GetInstances())
{
    if (service.GetPropertyValue("Name").ToString().ToLower().Equals("serviceName"))
    {
        if (service.GetPropertyValue("State").ToString().ToLower().Equals("running"))
        {
            //do something
        }
        else
        {
            //do something
        }
    }
}
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Ammar Ali
  • 77
  • 1
  • 3
  • 15
  • See [Why avoid string.ToLower() when doing case-insensitive string comparisons?](https://stackoverflow.com/q/28440783/1364007) – Wai Ha Lee Jan 11 '21 at 16:36