安装程序项目-使用提升的权限启动自定义操作

时间:2019-05-05 12:07:10

标签: c# visual-studio-2017 windows-installer custom-action

我有一个Visual Studio 2017 c#应用程序,其中带有安装程序项目安装程序。我们使用“自定义操作”启动可执行文件,该可执行文件在MSI完成时运行,并且该自定义操作位于“自定义操作”标签中的“提交”下。

应用程序运行时,其Windows用户主体为NT AUTHORITY \ SYSTEM。

我自己运行该应用程序时,它的用户是我,MYDOMAIN \ MYUSER

因此,我试图获取它来提升这些权限,并且到目前为止,我并没有主要研究旧的stackoverflow问题,但发现了三种可能的解决方案,但它们都不适合我,在所有情况下exe仍在NT AUTHORITY下运行\系统

  1. 添加包含以下内容的应用清单
<requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
  1. 在记事本中编辑vdproj文件,使RequiresElevation为“ true”
    "MsiBootstrapper"
    {
        "LangId" = "3:1033"
        "RequiresElevation" = "11:TRUE"
    }
  1. 尝试按照以下条件添加AdminUser作为启动条件:How do I avoid UAC when my EXE file name contains the word "update"?

以上内容已一起尝试过,但始终是SYSTEM用户。

关于如何使自定义操作以登录用户的特权而不是SYSTEM身份运行的任何想法?谢谢

1 个答案:

答案 0 :(得分:0)

EASY 答案原来是该线程中可接受的答案:

Windows installer using the NT AUTHORITY\SYSTEM instead of login user

[quote]

简短的答案是,您无法在作为InstallAllUsers安装程序的Visual Studio安装程序中执行此操作,因为所有VS安装程序生成的自定义操作均作为系统帐户运行。因此,您需要使用Orca等编辑工具来更改MSI文件中的自定义操作设置。您会在MSI文件的CustomAction表中找到该自定义操作,查看Type值(它可能是3074类型),然后关闭msidbCustomActionTypeNoImpersonate位,使其以安装用户的身份运行。

https://msdn.microsoft.com/en-us/library/windows/desktop/aa368069(v=vs.85).aspx

请注意,模拟为安装用户的运行有其自身的问题,因为它与以交互用户的运行不同。用户配置文件未加载,因此与用户关联的对象(例如HKCU,用户配置文件文件夹)非常不可靠。

许多人在首次启动应用程序时用单独的程序填充数据库,以便它可以作为交互式用户正常运行,并且可以作为独立程序进行开发和调试。如果在安装过程中填充失败,则可以放弃安装并回滚,或者继续安装并最终得到一个空数据库,为此您可能仍需要一个程序来填充它。 [/ quote]

不太容易回答,但出色的解决方案如下:

编辑MSI文件,以从自定义操作记录中删除msidbCustomActionTypeNoImpersonate类型。

我在VB.NET程序中以编程方式执行了以下操作:

    Dim o As WindowsInstaller.Installer = CType(CreateObject("WindowsInstaller.Installer"), WindowsInstaller.Installer)

    Dim db As WindowsInstaller.Database
    db = o.OpenDatabase(fil.FullName, WindowsInstaller.MsiOpenDatabaseMode.msiOpenDatabaseModeDirect)

    Dim record As WindowsInstaller.Record = Nothing
    view = db.OpenView("select File.File, FileName From File")
    view.Execute(record)
    record = view.Fetch
    Dim bFound As Boolean = False
    While record IsNot Nothing
        Dim sFileName As String = record.StringData(2)
        If sFileName.EndsWith("MYCUSTOMACTIONEXE.exe", StringComparison.CurrentCultureIgnoreCase) = True Then
            bFound = True
            Exit While
        End If
        record = view.Fetch
    End While

    If bFound = True Then
        Dim sGUID As String = record.StringData(1)

        '   At time of writing this was changing a 3602 into a 1554, so removing msidbCustomActionTypeNoImpersonate 
        '   The record key was _65BF5279_D2EA_42C1_AC66_90A684817EE5 which is the custom action for MYCUSTOMACTIONEXE.exe


        view = db.OpenView("select Action, Type From CustomAction Where Source = '" & sGUID & "'")
        view.Execute(record)
        record = view.Fetch
        If record IsNot Nothing Then
            Dim sActionGUID As String = record.StringData(1)
            Dim sType As String = record.StringData(2)
            If sActionGUID IsNot Nothing AndAlso sActionGUID <> "" Then
                '   Take off Hex 800 which means noimpersonation
                Dim lType As Integer = CInt(sType)
                If lType And 2048 = 2048 Then
                    Dim sNewType As String = CStr(lType - 2048)
                    Dim v As WindowsInstaller.View = db.OpenView(
                        "update CustomAction set Type=" & sNewType & " Where CustomAction.Action = '" & sActionGUID & "'")
                    v.Execute()
                End If
            End If

        End If
    End If