14

我有一个自托管的 WCF 服务应用程序,我想通过 msi 安装程序包部署它。端点使用 http 端口 8888。为了在安装后在 windows 2008 下启动项目,我必须以管理员身份运行程序或必须使用 netsh 编辑 http 设置:

"netsh http add urlacl url=http://+:8888/ user=\Everyone"

我想从我的安装程序类中编辑 http 设置。因此,我从 Install() 方法中调用以下方法:

    public void ModifyHttpSettings()
    {
        string parameter = @"http add urlacl url=http://+:8888/ user=\Everyone";

        System.Diagnostics.ProcessStartInfo psi =
            new System.Diagnostics.ProcessStartInfo("netsh", parameter);

        psi.Verb = "runas";
        psi.RedirectStandardOutput = false;
        psi.CreateNoWindow = true;
        psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        psi.UseShellExecute = false;
        System.Diagnostics.Process.Start(psi);
    }

此方法适用于英文版的 windows,但不适用于本地化版本(本地化版本中,Everyone 组有不同的名称)。我还尝试使用 Environment.UserName 至少允许当前登录的用户访问。但这也不起作用,因为安装程序类是由在用户 SYSTEM 下运行的 msi 服务运行的。因此 Enviroment.UserName 返回 SYSTEM ,这不是我想要的。

有没有办法从 msi 安装程序类授予所有(或至少当前登录的)用户对我的自托管 WCF 服务的访问权限?

4

1 回答 1

25

我的解决方案:

    public void ModifyHttpSettings()
    {
        string everyone = new System.Security.Principal.SecurityIdentifier(
            "S-1-1-0").Translate(typeof(System.Security.Principal.NTAccount)).ToString();

        string parameter = @"http add urlacl url=http://+:8888/ user=\" + everyone;

        ProcessStartInfo psi = new ProcessStartInfo("netsh", parameter);

        psi.Verb = "runas";
        psi.RedirectStandardOutput = false;
        psi.CreateNoWindow = true;
        psi.WindowStyle = ProcessWindowStyle.Hidden;
        psi.UseShellExecute = false;
        Process.Start(psi);
    }

SID“S-1-1-0”是众所周知的 SID,代表“所有人”帐户。对于 Windows 的所有本地化,SID 都是相同的。SecurityIdentifier 类的 Translate 方法返回每个人帐户的本地化名称。

于 2010-03-26T10:27:02.703 回答