How do I set ACL for a Windows Service in .net?

前端 未结 2 1113
情书的邮戳
情书的邮戳 2021-01-14 10:35

I have a service that I need to be able to start and stop with a button. I am using a ServiceController in a seperate program and everything works as intended when I run thi

2条回答
  •  无人共我
    2021-01-14 11:10

    You have a few options:

    1) You can require that your application runs as an administrator. Every time your application starts, you will be prompted with the UAC (on Windows 7 and Vista) and your application will be elevated to the required level.

    Run .NET application as administrator

    2) Your application can request an elevation when the action is required to stop and start the service. It would do this by starting another application at a higher level and this other application will do the actual start and stop.

    How to elevate privileges only when required?

    3) Preferred option, IMHO - You should build your service to run all of the time but just not do anything other than listen for requests via TCP/IP, Named Pipes or some other communication mechanism. Your service can then start or stop a thread that performs the real work.

    4) You can modify the service rights. Here are some posts that give some information on this (I would still prefer option 3):

    Start / Stop a Windows Service from a non-Administrator user account

    http://msmvps.com/blogs/erikr/archive/2007/09/26/set-permissions-on-a-specific-service-windows.aspx

    http://fstaal01.home.xs4all.nl/swsc-us.html

    Update

    I have changed some text and added option 4 based on Harry's comment. It seems that there are ways of tweaking the permissions. These require administrator rights initially but if you bundle something like swsc (third link) with your installation, you could use it to set the rights for you. I am not sure if there are any license implications for doing this. Alternatively, you could use a variant of the code he pasted.

提交回复
热议问题