Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cant start nancy self host without admin rights

Tags:

c#

nancy

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?

like image 702
kroax Avatar asked Apr 17 '13 15:04

kroax


2 Answers

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.

like image 120
robpvn Avatar answered Sep 22 '22 00:09

robpvn


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/
like image 21
Ashtonian Avatar answered Sep 25 '22 00:09

Ashtonian