service - 作为用户帐户安装服务时如何验证用户?

标签 service wix

我正在使用 wix 创建安装程序。我需要安装一个服务作为安装过程中给出的用户帐户。但是,在安装服务之前,我如何验证用户和密码是否有效,并且是否有足够的权限来安装服务。有什么办法可以做到吗?

wix中的以下代码帮助我安装服务。

<ServiceInstall Id="AServiceInstall" DisplayName="myservice"     Name="myservice" ErrorControl="normal" Start="auto" Vital="yes" Type="ownProcess" Account="[ACCOUNT]" Password="[PASSWORD]">
            <ServiceDependency Id="x"></ServiceDependency>
          </ServiceInstall>

最佳答案

为了验证复杂的用户输入,我通常必须创建一个自定义操作来执行此操作。

下面是一个示例对话框,用于验证 Web 服务的 URL 并确保其可访问:

<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<Fragment>
    <CustomAction Id="ValidateProxyService" BinaryKey="XXX.CommServer.CustomActions.dll" DllEntry="ValidateProxyService" Return="check" />

    <UI>
      <Dialog Id="XxxInquiryServiceDlg" Width="370" Height="270" Title="CommService Setup">
        <Control Id="BannerBitmap" Type="Bitmap" X="0" Y="0" Width="370" Height="44" TabSkip="no" Text="!(loc.InstallDirDlgBannerBitmap)" />
        <Control Id="BannerLine" Type="Line" X="0" Y="44" Width="370" Height="0" />
        <Control Id="BottomLine" Type="Line" X="0" Y="234" Width="370" Height="2" />
        <Control Id="Description" Type="Text" X="25" Y="23" Width="280" Height="15" Transparent="yes" NoPrefix="yes" Text="Configure the settings for the xxx." />
        <Control Id="Title" Type="Text" X="15" Y="6" Width="210" Height="15" Transparent="yes" NoPrefix="yes" Text="{\WixUI_Font_Title}Configure xxx" />
        <Control Type="Text" Id="ProxyServiceLabel" Width="95" Height="10" X="11" Y="60" Text="XXX Proxy Service URL:" />
        <Control Type="Edit" Id="ProxyServiceTextBox" Width="244" Height="15" X="112" Y="58" Property="XXX_PROXY_SERVICE_URL" />
        <Control Id="Next" Type="PushButton" X="236" Y="243" Width="56" Height="17" Default="yes" Text="!(loc.WixUINext)">
          <Publish Event="DoAction" Value="ValidateProxyService" Order="1">1</Publish>
        </Control>
        <Control Id="Back" Type="PushButton" X="180" Y="243" Width="56" Height="17" Text="!(loc.WixUIBack)" />        
        <Control Id="Cancel" Type="PushButton" X="304" Y="243" Width="56" Height="17" Cancel="yes" Text="!(loc.WixUICancel)">
          <Publish Event="SpawnDialog" Value="CancelDlg">1</Publish>          
        </Control>
      </Dialog>
    </UI>
  </Fragment>
</Wix>

然后我写的自定义操作:

[CustomAction]
public static ActionResult ValidateProxyService(Session session)
{
    try
    {
        string proxyServiceUrl = session["XXX_PROXY_SERVICE_URL"];
        string message;

        if (string.IsNullOrWhiteSpace(proxyServiceUrl))
        {
            session["XXX_PROXY_SERVICE_VALID"] = string.Empty;
            MessageBox.Show(
                "The proxy service URL is required.",
                "Error",
                MessageBoxButtons.OK,
                MessageBoxIcon.Error);
            return ActionResult.Success;
        }
        else if (!ValidateUrl(proxyServiceUrl, out message))
        {
            if (MessageBox.Show(
                message,
                "Error",
                MessageBoxButtons.OKCancel,
                MessageBoxIcon.Warning) == DialogResult.Cancel)
            {
                session["XXX_PROXY_SERVICE_VALID"] = string.Empty;
                return ActionResult.Success;
            }
        }

        session["XXX_PROXY_SERVICE_VALID"] = "1";
    }
    catch (Exception ex)
    {
        session.Log("An error occurred during ValidateProxyService: {0}", ex);
        return ActionResult.Failure;
    }

    return ActionResult.Success;
}

所发生情况的总体概述是对话框请求 URL 并将其放入 XXX_PROXY_SERVICE_URL 属性中。当您按对话框上的“下一步”按钮时,它将运行自定义操作。然后,自定义操作读取 XXX_PROXY_SERVICE_URL 并测试它是否有效并尝试加载页面。如果无效或失败,则会取消设置 XXX_PROXY_SERVICE_VALID 属性。

在安装程序的主 UI 中,我有:

<Publish Dialog="XXXServiceDlg" Control="Next" Event="NewDialog" Value="OtherDlg" Order="2">XXX_PROXY_SERVICE_VALID</Publish>

因此,如果服务无效,它将不会进入下一页。

一些注意事项:

  • 我只是显示一个警告对话框,并询问他们是否要继续(是/否),即使输入无效,在服务现在无法访问但他们仍然需要完成安装的情况下。<
  • 我真的很想使用 WIX 对话框来处理消息对话框。由于我无法获取安装程序窗口的句柄,因此在某些我仍不知道的情况下,使用 MessageBox 类往往会在安装程序下弹出。我还认为在 WIX 中处理所有 UI 会更清晰。
  • 某些需要验证的有用内容需要管理员访问权限。这会在生成 MSI 时产生烦人的可用性影响。要以管理员身份运行 MSI 的 UI 部分,需要通过具有管理员访问权限的命令行启动它,或者生成一个 Bootstrap (.exe),该 Bootstrap 要么被用户告知以管理员身份运行,要么配置为以管理员访问权限启动.

需要填写的一些空白:

  • 在 WIX 中使用自定义操作时缺少的所有详细信息。
  • 检查用户凭据是否有效及其权限的确切方法。

一旦弄清楚这两件事,调整上述策略来验证任何任意用户输入应该不会太困难。

关于service - 作为用户帐户安装服务时如何验证用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23464925/

相关文章:

android - Intent 始终为 null onStartCommand

android - MediaPlayer 应该在单独的线程中运行吗?

android - 服务不停

android长时间运行的服务没有通知

c# - 以用户身份安装 Windows 服务

visual-studio-2010 - .NET 4.5 的 WiX Bootstrap

wix - 使用 Microsoft WIX 安装命名空间扩展

iis - WiX-Bundle 安装后如何激活 IIS 功能?

Wix 'Advanced' 安装不卸载以前的版本

WiX Visual Studio 安装项目 : How to build different msi in one project, 取决于预处理器变量