13

Running this code as a regular user throws HttpListenerException (access denied). Snippet runs ok as an administator

class Program
{
    static void Main(string[] args)
    {
        HttpListener listener = new HttpListener();
        listener.Prefixes.Add("http://myip:8080/app/");
        listener.Start();
        //.... and so on
     }
}

i went ahead and added the uri using netsh (netsh http show lists the uri)

netsh http add urlacl url=http://+:8080/app user=domain\user

still getting the same error. Adding ACLs did work for other projects (they didn't use HttpListener though). I tried multiple port/application name combinations, nothing works.

Any ideas what might be the cause?

Running .Net 3.5 SP1 on Vista

smvlad
  • 323
  • 1
  • 3
  • 13

2 Answers2

15

I do not understand why but here it is. It seems that the cause is that my network card is configured with 2 IPs.

if in the code i specify one of the ips (like i did in question above)

listener.Prefixes.Add("http://myip1:8080/app/");

then to avoid exception i need to register it with IP-bound weak wildcard

netsh http add urlacl url=http://myip1:8080/app user=domain\user

however if i add prefix with the strong wildcard (plus sign)

listener.Prefixes.Add("http://+:8080/app/");

and register with the same wild card

netsh http add urlacl url=http://+:8080/app user=domain\user

then there is no error and i can access my app from both ip.

smvlad
  • 323
  • 1
  • 3
  • 13
  • Note that putting `netsh http add urlacl url=http://+:PORT_NUMBER/ user=.\Users` is likely to work for all cases, for any user on that system, including domain users (as all members of DOMAIN\Users typically belong to local .\Users group, but not vice versa). – zmilojko Feb 19 '13 at 13:50
0

Is that URI already registered on the system?

http://msdn.microsoft.com/en-us/library/system.net.httplistenerexception.aspx says that would be one cause.

BQ.
  • 9,393
  • 3
  • 25
  • 35