ASP.NET Web API Self-Host both HTTPS and HTTP in one service?

I have a publicly accessible ASP.NET Web API service that provides two controllers. I would like to get one of them only through HTTPS, and the other - no. Can this be done in one service? If so, can you give some advice? It looks like I will need to register two base addresses, but I do not see how this is possible for one service.

+4
source share
2 answers

You need to create two instances of HttpServer, one for http and one for https. I tried to find out why this restriction exists, because I know that the HttpListener can handle registration as for the same listener.

In any case, if creating two instances of HttpServer is really not right for you, you will need to look at the Katana project and Microsoft.Owin.HttpListener. This supports multiple addresses, but unfortunately Katana's startup code does not work by default! But I'm sure there is a way to configure the HttpListener at startup to make this possible.

+6
source

Since the HttpListenerBase (from ServiceStack.Host.HttpListener) does not override its Listener, if it is non-null at startup, the following can be done in your host class: HttpListenerBase:

public override ServiceStackHost Start(string urlBaseNoPrefix) { // Here urlBaseNoPrefix is something like "web.acme.com/app/" Listener = new System.Net.HttpListener(); Listener.Prefixes.Add("http://" + urlBaseNoPrefix); base.Start("https://" + urlBaseNoPrefix); return this; } 
0
source

All Articles