How can i prompt for password and user when someone tries to stop a windows service?
-
9Why on earth would you want that? But no, there is no direct way to do it, since Windows Services aren't tied to the Windows GUI. – Quintium Jun 07 '12 at 18:54
-
1It is assumed that anyone with the privileges required to stop a service has administrative controls and can pretty much do whatever they want. No additional password should be necessary. – Mark Ransom Jun 07 '12 at 19:01
-
1Check this out: [Protecting a Windows Service from untrusted users](http://stackoverflow.com/questions/4266776/protecting-a-windows-service-from-untrusted-users) – Thomas C. G. de Vilhena Jun 07 '12 at 19:02
-
Going about this the wrong way. Services should be locked down to admins only. If someone is the admin on their machine they should be allowed to start and stop whatever they want. – Tony318 Jun 07 '12 at 19:05
-
I want do this as a windows service project. Isn't there any solution to this? in c# – AlexandruC Jun 07 '12 at 19:08
-
Read comment 1 with 8+ up votes. A Windows Service does not have a UI. You could write a console application that listens on a port like a service. And you could ask for a password if they tried to stop the program. But the user could still go to Task Manager and kill the process. What are you trying to do? – paparazzo Jun 07 '12 at 19:17
-
I want to restrict the users from stopping a a custom made service, that I made, in c# based on a password. – AlexandruC Jun 07 '12 at 19:38
3 Answers
It cannot be done.
Services are meant to run outside the user experiance and do not handle GUI interactions. This is something that is left up to the operating system to allow or disallow a user from stopping a service.
- 26,526
- 10
- 91
- 107
It cannot be done directly from the service. However, the service can be managed by another application if the service is set to interact with the desktop. So you could create a second application with a GUI that monitored the service. The service could set some value in shared state file when the services 'Stopping' event fires and wait until the monitoring app writes a confirmation value in the state file. Maybe not what you are looking for exactly but I think you could get the result that you want.
- 581
- 4
- 18
-
I wrote a program years ago that did some of this stuff. I'll see if I can find it for you. – Dusty Lau Jun 07 '12 at 19:39
-
-
But what if the GUI application is to be stopped by the user? I don't want that. – AlexandruC Jun 07 '12 at 19:53
-
Take a look at the ServiceContoller class. It is what you should probably use in your monitoring application. Your service can monitor your monitoring app and your monitoring app monitors your service. If your service sees that the monitor is not running it launches it. Your monitor app should use ServiceController.WaitForStatus() or a loop/timer of some sort that watches the ServiceController.Status property. The cleanest way to accomplish what you want is to probably let the monitor watch for StopPending or Stopped, Prompt the user and then ServiceController.Start or do nothing. – Dusty Lau Jun 07 '12 at 20:08
-
I cannot find the project that I did some of this is. It might be buried on one of my older dev boxes I'll let you know if I find it. – Dusty Lau Jun 07 '12 at 20:09
-
1If a user has the rights to stop the service or the monitor they can. That might be harder to stop. But if the service periodically checks to make sure the monitoring app is running and starts it if its not... that gets you half way there. – Dusty Lau Jun 07 '12 at 20:13
-
1There's no need for a GUI process to be running all the time, just have the user launch the process if they want to stop the service. – Harry Johnston Jun 07 '12 at 20:20
-
-
A system tray app is appropriate if the service interacts with the user in other ways, but not just to provide a shutdown option. Either way, the important point is that it doesn't matter if the user stops the GUI application as the service will continue to run. – Harry Johnston Jun 08 '12 at 00:11
Ordinarily, there's no reason to do this. By default, only administrators can stop a service, and if the service can be stopped at all it makes no sense to ask an administrator for a password to do so: they're an administrator, so by definition they're entitled to do anything.
The one scenario that makes sense is if you want ordinary users to be able to stop the service if they know the password. That way, you can let someone stop the service without giving them administrative rights to the computer. (Even then, in most cases it would be simpler to change the permissions on the service to allow the user(s) in question the right to stop the service; but perhaps, for example, you want users to be have to phone a helpdesk to be given the password.)
The secret to making this work is that the service is entitled to stop itself for any reason without having received a stop request from the operating system. So you can just write a program that the users can run if they want to stop the service. The program accepts the password and sends it to the service over some form of IPC, such as a named pipe. If the password is correct, the service stops.
You could also configure the service so that it doesn't accept stop requests, in which case an administrator would also need the password in order to stop the service nicely. But that wouldn't stop them from stopping the service by killing the service process, or uninstalling the service and rebooting the computer.
- 34,474
- 6
- 59
- 151