没有管理员权限不能启动Nancy self host
本文关键字:Nancy self host 启动 不能 管理员 权限 | 更新日期: 2023-09-27 18:09:46
我的应用程序使用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时,我没有得到任何异常。是什么导致了这个异常?
您需要通过RewriteLocalhost
属性设置self-host配置,使其不重写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();
}
}
}
}
我通过尝试和失败发现了这个问题,但是这个页面解释了背后的原因。
从文档中选择:
注意,在Windows主机上,HttpListenerException可能会抛出Access Denied消息。要解决这个问题,必须将URL添加到ACL中。此外,该端口可能需要在机器或公司防火墙上打开,以允许访问该服务。
执行如下命令加入ACL。
netsh http add urlacl url=http://+:8080/ user=DOMAIN'username
如果需要从ACL中删除:
netsh http delete urlacl url=http://+:8080/
你可以用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来解析所有的依赖项。