简体   繁体   English

尝试将URL添加到ACL的HttpListener“访问被拒绝”

[英]HttpListener “Access denied” with trying to add url to ACL

I have a WPF application using HttpListener, but whenever I start listening I get an "access denied" exception due to not running the app as administrator. 我有一个使用HttpListener的WPF应用程序,但是每当我开始监听时,由于没有以管理员身份运行该应用程序,都会收到“拒绝访问”异常。 I have the following code which will restart the server in case the user is not an admin and start the server with elevated privileges but I suspect it won't work unless the end user has administrator rights. 我有以下代码,如果用户不是管理员,它将重新启动服务器,并以提升的特权启动服务器,但是我怀疑除非最终用户具有管理员权限,否则它将无法工作。

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;
            }

I've read that HttpListener uses the kernel driver and opening listeners here is secured by the ACL, so I need to set up the ACL using netsh, but I'm a little clueless as to how I do this. 我已经了解到HttpListener使用内核驱动程序,并且此处打开的侦听器受ACL的保护,因此我需要使用netsh来设置ACL,但是我对如何做到这一点一无所知。 I've tried doing the following which makes the UAC prompt me about allowing the program to make changes to my computer, but I still get an access denied when trying to start the listener? 我尝试执行以下操作,这会使UAC提示我有关允许程序对计算机进行更改的信息,但是尝试启动侦听器时仍会拒绝访问?

I'm calling AddAddress(localIp) after initializing the main window. 初始化主窗口后,我正在调用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();
    }

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM