简体   繁体   English

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

[英]cant start nancy self host without admin rights

My app uses Nancy Selfhosting. 我的应用程序使用Nancy Selfhosting。 When I launch it without admin rights I get a System.Net.HttpListenerException "Access Denied". 当我在没有管理员权限的情况下启动它时,我得到一个System.Net.HttpListenerException“Access Denied”。

Here is the code: 这是代码:

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

I have also tried different ports without success. 我也试过不同的端口但没有成功。 Strangely, I dont get any Exceptions when launching a HttpListener that listens to the same Url. 奇怪的是,在启动侦听同一个Url的HttpListener时,我没有得到任何异常。 What could be causing this exception? 可能导致此异常的原因是什么?

You need to set the self-host configuration to not rewrite the localhost route via the RewriteLocalhost property . 您需要将自主机配置设置为不通过RewriteLocalhost属性重写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();
            }
        }
    }
}

I found this out by trying and failing a bit, but this page explains the reason behind. 我通过尝试和失败来发现这一点,但这个页面解释了背后的原因。

Alternatively - From the documentation : 或者 - 从文档

Note that on Windows hosts a HttpListenerException may be thrown with an Access Denied message. 请注意,在Windows主机上,可能会抛出带有“拒绝访问”消息的HttpListenerException。 To resolve this the URL has to be added to the ACL. 要解决此问题,必须将URL添加到ACL。 Also but the port may need to be opened on the machine or corporate firewall to allow access to the service. 此外,可能需要在计算机或公司防火墙上打开端口以允许访问该服务。

Add to ACL by running the following command: 通过运行以下命令添加到ACL:

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

if you need to remove from ACL: 如果你需要从ACL中删除:

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

You can hosting Nancy with Kestrel. 你可以用Kestrel主持Nancy。 It's really simple: 这很简单:

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

The only difficulty is to prepare all the dlls (30+) required. 唯一的困难是准备所需的所有dll(30+)。 We should definitely use NuGet to resolve all the dependencies. 我们绝对应该使用NuGet来解决所有依赖关系。

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

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