16

I'm developing an application that is so far using HttpListener to provide a small standalone http server. However, I've recently discovered that HttpListener needs to be run as Administrator, which is not always going to be possible.

What would the best alternative be? I need http GET and POST, both of which are not simply reading/writing files on the filesystem, they need to run custom .Net code.

My research so far has brought up Cassini, but as far as I can tell, I would have to write a custom version. Is there anything else? In partiular something with the same interface as HttpListener, but that does not require Administrator privileges would be amazing!

Grokys
  • 16,228
  • 14
  • 69
  • 101
  • 10
    HttpListener doesn't need to be run as admin if you do the right "netsh http add urlacl ..." stuff first – Will Dean Dec 20 '09 at 16:22
  • 3
    The netsh route is such a terrible concept. This is truly a Windows-ism. Firing up an HTTP listener in .NET shouldn't be so horribly convoluted and fraught with deployment concerns. – John Hargrove Jul 09 '19 at 17:13

8 Answers8

8

One alternative that I've found is C# Webserver on CodePlex.

"... a flexible http server which can be embedded in any .Net application. It has a modular design where features are added using modules. The server also supports REST and all http verbs ..."

It has an HttpListener class which I imagine is similar to System.Net.HttpListener, but I haven't used either one of them yet so I can't be certain.

  • I believe it is socket-based, not needing admin priveleges. –  Nov 13 '09 at 14:14
  • woops, Chris Kimpton's link points to the same project, only on CodeProject instead of CodePex. –  Nov 13 '09 at 14:24
4

One solution to this is covered by this other question - you can give yourself permissions to run HttpListener as a non-admin.

You could get the app to be started from a command file that sorts out the permissions and then runs the real app.

Community
  • 1
  • 1
Chris Kimpton
  • 5,546
  • 6
  • 45
  • 72
  • 1
    This isn't really an option as the application needs to be as easily installable as a regular desktop application. – Grokys Nov 11 '08 at 11:55
  • I've gone down the netsh route. I'm assuming I can run the netsh command as part of the installer. Many installers request elevated permissions, even when the application doesn't. – Greg Woods Aug 17 '11 at 22:01
  • This is a helpful answer but the fact that this is needed is a travesty – John Hargrove Jul 09 '19 at 17:17
3

Regarding the statement:

I've recently discovered that HttpListener needs to be run as Administrator

That's not entirely true and some of the other answers touch on one of the reasons, but there is another:

  1. Noted by other posters: You can grant permissions to a non-Administrator. Fine, but not great.
  2. You can listen on localhost, even on port 80, without being an admin. Note: I recall that only "localhost" works and not "127.0.0.1"... so be sure that you pass localhost to your Prefixes.Add call.

We're shipping an internal tool that allows developers to run an HTTP-based app host on their PCs and we at first thought using HttpListener would not be possible due to the Admin (or Admin-granted permissions) problem, but then we found that localhost works just fine without being an Admin. It sort of makes sense: listening externally is "dangerous" but listening on the local machine is not quite as dangerous.

chwarr
  • 6,777
  • 1
  • 30
  • 57
huntharo
  • 2,616
  • 21
  • 23
3

Like the comment by Will Dean on your post says, you could run the following netsh command:

netsh http add urlacl url=http://+:8346/ user="NTAuthority\Authenticated Users" sddl="D:(A;;GX;;;AU)"

Substitute the 'http://+:8346/' with your value, and this will allow any authenticated user to run the webserver at the target endpoint.

ctrlplusb
  • 12,847
  • 6
  • 55
  • 57
  • This didn't work for me; it results in the system listening for HTTP on the given port, but refusing all connections with 503 Service Unavailable. – jimrandomh Aug 09 '12 at 20:51
  • @jimrandomh - Try put in your full domain for the registration. Example: `netsh http add urlacl url=http://www.mytestdomain.com:8346/ user="NTAuthority\Authenticated Users" sddl="D:(A;;GX;;;AU)"` [Also, you can check/modify your url reservations with this tool - http://urlreservation.codeplex.com/](http://urlreservation.codeplex.com/) – ctrlplusb Aug 10 '12 at 10:24
  • And don't forget your firewall settings too. :) Your firewall may be blocking connections. Try disable it temporarily and then try connect again to ensure that this is not the issue. – ctrlplusb Aug 10 '12 at 10:27
1

For .NET Core applications there exists a new (cross-platform) HTTP server implemention: Kestrel.

CodeFox
  • 3,321
  • 1
  • 29
  • 41
0

Ok, so you have a regular desktop app that needs to allow inbound http connections - hmm - won't windows firewall be an issue?

Assuming not, it sounds almost like a webservice - could you go that route - expose the URLs via that? Although my .Net knowledge is not deep enough to know if you still need to run a specific http server to answer requests. Spring.Net is probably worth a look.

Chris Kimpton
  • 5,546
  • 6
  • 45
  • 72
  • No, it will only be receiving connections from localhost - the application is a proxy for an Adobe AIR application which is installed at the same time. – Grokys Nov 11 '08 at 12:23
  • Also, I've considered webservices but the server will be mainly serving binaries, which is not a good fit for webservices. – Grokys Nov 11 '08 at 12:26
0

Nowin is a great lib that can be integrated in Owin as ServerFactory without dependency on HttpListener. Were able to drop in replace Microsoft.Owin.Host.HttpListener lib without a hitch

AlfeG
  • 1,475
  • 4
  • 18
  • 33
0

As of 2023, there's now a new alternative: SpaceWizzards.HttpListener.HttpListener is a 1:1 replacement of the existing System.Net.HttpListener. The nuget is available as SpaceWizards.HttpListener. Despite its low version number of 0.1.0 it already has over 400k downloads at the time of this writing. A first test shows that it's actually working as expected.

PMF
  • 14,535
  • 3
  • 23
  • 49