【问题标题】:HttpListener "Access denied" with trying to add url to ACLHttpListener“访问被拒绝”,尝试将 url 添加到 ACL
【发布时间】:2014-10-30 08:49:55
【问题描述】:

我有一个使用 HttpListener 的 WPF 应用程序,但每当我开始监听时,由于没有以管理员身份运行应用程序,我会收到“拒绝访问”异常。我有以下代码,如果用户不是管理员,它将重新启动服务器并以提升的权限启动服务器,但我怀疑它不会工作,除非最终用户具有管理员权限。

private static bool IsAdmin()
    {
        WindowsIdentity identity = WindowsIdentity.GetCurrent();
        WindowsPrincipal principal = new WindowsPrincipal(identity);
        return principal.IsInRole(WindowsBuiltInRole.Administrator);
    }


    private void StartServerButton_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            if (IsAdmin() != true)
            {
                MessageBox.Show("You need admin privileges to run this server. Restarting server as admin.");
                var programName = Process.GetCurrentProcess().MainModule.FileName;
                ProcessStartInfo startInfo = new ProcessStartInfo(programName);
                startInfo.Verb = "runas";
                Process.Start(startInfo);
                Application.Current.Shutdown();
                return;
            }

我读到 HttpListener 使用内核驱动程序,并且此处打开的侦听器受 ACL 保护,因此我需要使用 netsh 设置 ACL,但我对如何执行此操作有点无能为力。我已尝试执行以下操作,这使 UAC 提示我允许程序对我的计算机进行更改,但在尝试启动侦听器时我仍然收到拒绝访问?

我在初始化主窗口后调用 AddAddress(localIp)。

 public void AddAddress(string address)
    {
        AddAddressToAcl(address, Environment.UserDomainName, Environment.UserName);
    }

    public static void AddAddressToAcl(string address, string domain, string user)
    {
        string args = string.Format(@"http add urlacl url={0} user={1}\{2}", address, domain, user);

        ProcessStartInfo startInfo = new ProcessStartInfo("netsh", args);
        startInfo.Verb = "runas";
        startInfo.CreateNoWindow = true;
        startInfo.WindowStyle = ProcessWindowStyle.Hidden;
        startInfo.UseShellExecute = true;

        Process.Start(startInfo).WaitForExit();
    }

【问题讨论】:

    标签: c# http user-controls


    【解决方案1】:

    似乎我遇到的问题是我给 AddAddress() 的地址是错误的。

    【讨论】:

      猜你喜欢
      • 2011-04-30
      • 2017-03-17
      • 1970-01-01
      • 2016-05-03
      • 1970-01-01
      • 1970-01-01
      • 2021-07-08
      • 2022-01-01
      • 1970-01-01
      相关资源
      最近更新 更多