18

My app uses Nancy Selfhosting. When I launch it without admin rights I get a 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. What could be causing this exception?

08Dc91wk
  • 4,254
  • 8
  • 34
  • 67
kroax
  • 249
  • 3
  • 10
  • Is something running on port 80 already? – Ritch Melton Apr 17 '13 at 15:56
  • 1
    I think your `HttpListener` test is flawed. I get Access Denied during `.GetContext()` when I'm not elevated on windows 7. –  Apr 17 '13 at 16:53
  • Well of course theres some more application logic in my Programm. And the whole Application works perfectly with a "self - build" Webserver built around the HttpListener. I just wanted to use nancy because of expandability etc. But having to start it with admin rights could be a nogo for our customer. – kroax Apr 18 '13 at 08:08

3 Answers3

47

You need to set the self-host configuration to not rewrite the localhost route via the RewriteLocalhost property.

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.

user2864740
  • 60,010
  • 15
  • 145
  • 220
robpvn
  • 897
  • 8
  • 18
4

Alternatively - From the documentation:

Note that on Windows hosts a HttpListenerException may be thrown with an Access Denied message. To resolve this the URL has to be added to the 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:

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

if you need to remove from ACL:

netsh http delete urlacl url=http://+:8080/
Ashtonian
  • 4,371
  • 2
  • 18
  • 25
0

You can hosting Nancy with Kestrel. 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. We should definitely use NuGet to resolve all the dependencies.

Jeffrey Zhao
  • 4,923
  • 4
  • 30
  • 52