[英]Programmatically grant local user rights to start a service with .Net
我想在我的應用程序中實現更新服務(因此,用戶在安裝該服務后就不需要管理員權限來更新我的應用程序,就像Google或Mozilla進行更新一樣),我認為我找到了一種執行此操作的好方法與WCF。
我有一個WCFServiceLibrary -Project,其中包含ServiceContract和核心功能(下載/安裝更新),還有一個Windows Service-Project ,該項目將WCFServiceLibrary實現為Windows Service。 此外,還有一個Visual Studio Installer -Project,用於安裝服務和我的應用程序,該應用程序應該能夠使用NamedPipes啟動/停止/與服務通信。
該服務配置為使用LocalSystem-Account手動啟動。 現在,當安裝了服務時,我可以使用services.msc(可能已提升)啟動/停止它,但是當我使用net start Servicename(錯誤5:訪問被拒絕)或我的應用程序嘗試它時,則不能啟動/停止它,這告訴我本地用戶可能沒有啟動/停止服務的權限。
我需要該服務以更高的權限運行才能執行更新的安裝,因此我想授予本地用戶在服務的首次安裝期間或服務首次啟動時啟動我的服務的權限(因為我也可以在安裝過程中觸發它。
但是,如何用VB.NET(或C#)完成此操作? 我發現了一些使用advapi32.dll的API調用的示例,但似乎無法使用此權限來更改權限。
因此,長話短說,以下是我所尋找的摘要:
這里已經存在不同的類似問題,但是沒有一個問題能給出明確的答案。 一個用戶可能是使用WiX-Installer來完成的,但是我想保留Visual Studio Installer項目,因為它非常簡單易用。
經過更多的谷歌搜索並嘗試找到一種“干凈”的解決方案之后,我放棄了,現在使用Process.Start
執行sc.exe並在安裝后設置新的權限。
這是我的ServiceInstaller-Class,適合所有好奇的人:
[VB.NET]
Imports System.ComponentModel
Imports System.Configuration.Install
Imports System.ServiceProcess
<RunInstaller(True)>
Public Class SvcInstaller
Inherits Installer
Dim svcprocinst As ServiceProcessInstaller
Dim svcinst As ServiceInstaller
Public Sub New()
svcprocinst = New ServiceProcessInstaller
svcprocinst.Account = ServiceAccount.LocalSystem
svcinst = New ServiceInstaller
svcinst.ServiceName = "KrahMickeySvc"
svcinst.DisplayName = "Mickey-Service"
svcinst.Description = "This Service is used by KRAH Mickey for application updates and maintenance"
Installers.Add(svcprocinst)
Installers.Add(svcinst)
End Sub
Private Sub SvcInstaller_AfterInstall(sender As Object, e As InstallEventArgs) Handles Me.AfterInstall
'Set new permissions acc. to Security Descriptor Definition Language (SDDL)
'Source: https://blogs.msmvps.com/erikr/2007/09/26/set-permissions-on-a-specific-service-windows/
'Keeping the source DACL and just adding RP,WP and DT (Start/Stop/PauseContinue) to IU (Interactive User)
Dim DACLString As String = "D:(A;;CCLCSWRPWPDTLOCRRC;;;SY)(A;;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;BA)(A;;CCLCSWLOCRRCRPWPDT;;;IU)(A;;CCLCSWLOCRRC;;;SU)"
process.Start("sc.exe", $"sdset {svcinst.ServiceName} ""{DACLString}""")
End Sub
End Class
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.