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

时间:2013-04-17 15:54:50

标签: 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();
    }

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

3 个答案:

答案 0 :(得分:44)

您需要将自主机配置设置为不通过RewriteLocalhost property重写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();
            }
        }
    }
}

我通过尝试和失败来发现这一点,但是this page explains the reason behind.

答案 1 :(得分:4)

或者 - 来自documentation

  

请注意,在Windows主机上,可能会抛出带有“拒绝访问”消息的HttpListenerException。要解决此问题,必须将URL添加到ACL。   此外,可能需要在计算机或公司防火墙上打开端口以允许访问服务。

通过运行以下命令添加到ACL:

netsh http add urlacl url=http://+:8080/ user=DOMAIN\username

如果您需要从ACL中删除:

netsh http delete urlacl url=http://+:8080/

答案 2 :(得分:0)

您可以使用Kestrel主持Nancy。这很简单:

public void Main(string[] args)
{
    var owinHost = new WebHostBuilder()
        .UseStartup<Startup>()
        .UseUrls("http://+:12345/")
        .Build();

    owinHost.Run();
}

public class Startup
{
    public void Configure(IApplicationBuilder app)
    {
        app.UseOwin(x => x.UseNancy());
    }
}

唯一的困难是准备所需的所有dll(30+)。我们绝对应该使用NuGet来解决所有依赖关系。

相关问题