无法启动nancy self host而没有管理员权限

kro*_*oax 17 c# nancy

我的应用程序使用Nancy Selfhosting.当我在没有管理员权限的情况下启动它时,我得到一个System.Net.HttpListenerException"Access Denied".

这是代码:

static void Main(string[] args)
    {   
        var nancyHost = new Nancy.Hosting.Self.NancyHost(new Uri("http://localhost:80/"));
        nancyHost.Start();
        Application.Run();
    }
Run Code Online (Sandbox Code Playgroud)

我也试过不同的端口但没有成功.奇怪的是,在启动侦听同一个Url的HttpListener时,我没有得到任何异常.可能导致此异常的原因是什么?

rob*_*pvn 46

您需要将自主机配置设置为不重写localhost路由.

namespace NancyApplication1
{
    using System;
    using Nancy.Hosting.Self;

    class Program
    {
        static void Main(string[] args)
        {
            var uri = new Uri("http://localhost:3579");
            var config = new HostConfiguration();

            // (Change the default RewriteLocalhost value)
            config.RewriteLocalhost = false;

            using (var host = new NancyHost(config, uri))
            {
                host.Start();

                Console.WriteLine("Your application is running on " + uri);
                Console.WriteLine("Press any [Enter] to close the host.");
                Console.ReadLine();
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我通过尝试和失败来发现这一点,但这个页面解释了背后的原因.