通过程序授予本地用户权利,以使用.NET启动服务



我想在我的应用程序中实现更新服务(因此,一旦安装了服务,用户不需要管理员就可以更新我的应用程序,就像Google或Mozilla一样(,而且我认为我找到了使用WCF进行此操作的好方法。

我有一个 wcfservicelibrary -project,其中包含ServiceCecntract和核心功能(下载/安装更新(和 Windows Service-service-project ,它实现了WCFServicElibrary作为Windows的Windows服务。此外,还有一个安装服务和我的应用程序的Visual Studio Installer -project,它应该能够使用名为Pipes启动/停止/停止/停止/进行通信。

该服务配置为用当地系统元手动启动。现在,当安装服务时,我可以使用services.msc(可能提高(启动/停止它,但是当我尝试使用Net start ServiceName(错误5:访问拒绝(或使用我的应用程序尝试时,我可以告诉我本地用户可能没有启动/停止服务的权限。

我需要该服务以更高的权限运行以执行更新的安装,因此我想在第一次安装服务或服务首次启动(因为我可以在安装过程中触发它(。

但是,如何使用vb.net(或c#(完成此操作?我发现了一些使用Advapi32.dll的api-calls的示例,但是看起来可以更改此权限。

所以,长话短说,这是我要寻找的内容的摘要:

  • 授予组"本地用户"的权限,可以在安装过程中开始我的服务,最佳方法(也许在Visual Studio Installer项目中使用自定义操作?或Windows Service-roject中的ServiceInstaller-class中的自定义操作?(或第一次是第一次服务启动(Windows Service Project中的Onstart-Event(
  • 该服务不得与本地用户权利运行,因为它会错过高架特权,然后安装更新。
  • 我无法通过GPO/本地政策分配权限,因为用户不在我们的公司内,而是全世界。出于同样的原因,我不能假设他们可以每次更新时都可以提升管理员来提升他们。
  • 如果可能的话,我想避免使用命令行调用(如通过命令行分配权限,因为这些最有可能是OS依赖的(
  • 另一个解决方案是将服务配置为自动,并在安装后启动它,但是我不喜欢我的服务一直运行的想法,因为仅当我的主应用程序启动时才需要它。
  • 它很可能不是文件权限问题。每个人,系统和服务都可以完全访问服务文件夹和文件。

这里已经存在不同的类似问题,但是没有人对此问题给出明确的答案。一个用户可能使用Wix-installer进行了操作,但是我想保留Visual Studio Installer项目,因为它非常简单易用。

在更多谷歌搜索并尝试找到"干净"解决方案之后,我已经放弃并使用Process.Start来执行SC.EXE并在安装后设置新的权限。p>这是我的ServiceInstaller级,对于任何好奇的人来说:

[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

最新更新