TransWikia.com

Access Denied when accessing Service controller from application

Server Fault Asked by kaka1234 on January 20, 2021

I have a web application written in c# mvc which is trying to check the status of windows service whether it is stopped or started. I have written the code but the issue is the code works fine on my local dev machine but when pushed to the server the code errors out stating access is denied.

I have the following code:

            try
            {
                using (ServiceController sc = new ServiceController("Service"))
                {
                    if (sc.Status == ServiceControllerStatus.Running)
                    {
                        //do something
                    }
                }
            }
               catch (Exception ex) { }; 
           } 

The above code keeps failing at Service controller itself.

I also tried to use the below code:

    StringBuilder sb = new StringBuilder();
    Process process = new System.Diagnostics.Process();
    ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();

    startInfo.FileName = @"sc";
    startInfo.Arguments = "query "My Service"";
    startInfo.UseShellExecute = false;
    process.StartInfo = startInfo;
    process.OutputDataReceived += (sender, args) => 
    sb.AppendLine(args.Data);
    process.StartInfo.RedirectStandardOutput = true;
    process.Start();
    process.BeginOutputReadLine();
    process.WaitForExit();

Both of the above code keep failing at the Service controller / SC itself.

The error is

Cannot open Service Control Manager on computer ‘.’. This operation might require other privileges.

Our application is running in IIS under service account and we have given admin rights and remote access rights to this account.

Not sure what else needs to be given here. Is there any specific rights to access windows service?

One Answer

Try running your code as 'LocalSystem', a good background there too; https://stackoverflow.com/questions/39326284/cant-query-run-or-stop-a-windows-service

Answered by yagmoth555 on January 20, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP