cant start nancy self host without admin rights

kroax picture kroax · Apr 17, 2013 · Viewed 8.2k times · Source

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?

Answer

robpvn picture robpvn · Aug 26, 2013

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.