I'm using HttpListener to allow a user to set up a proxy on a user-defined port. When I start the HttpListener, I get an exception if the application isn't running under administrator privileges in Vista.
From what I've read, this is expected behavior - administrator privileges are required to start listening on a port. But I'm sure there are ways around this, as I run plenty of programs (like Skype) which listen on a port without requiring elevation to administrator.
Is there a way to do this with HttpListener? If not, can I make other API calls in .NET code to set up the port?
While you can write your own HTTP server using normal TCP/IP (it's relatively simple), it is easier to use HttpListener, which takes advantage of the HTTP.SYS functionality added in Windows XP SP2.
However, HTTP.SYS adds the concept of URL ACLs. This is partly because HTTP.SYS allows you to bind to sub-namespaces on port 80. Using TCP/IP directly avoids this requirement, but means that you can't bind to a port that's already in use.
On Windows XP, you can use the HttpCfg.exe program to set up a URL ACL granting your user account the right to bind to a particular URL. It's in the Platform SDK samples.
On Windows Vista, HTTPCFG is still supported, but the functionality has been absorbed into NETSH:
netsh http show urlacl
...will show a list of existing URL ACLs. The ACLs are expressed in SDDL.
netsh http add urlacl url=http://+:80/MyUri user=DOMAIN\User listen=yes
...will configure the MyURI namespace so that DOMAIN\User can listen to requests.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With