ASP.NET Web API Self-Host both HTTPS and HTTP in s

2019-04-28 10:47发布

I have a public self-hosted ASP.NET Web API service that exposes two controllers. One of them I would like to have accessible only via HTTPS, and the other I don't. Can this be done within a single service? If so, can you provide a few hints? It seems like I would need to register two base addresses, but I don't see how that's possible for a single service.

2条回答
家丑人穷心不美
2楼-- · 2019-04-28 11:35

You need to create two HttpServer instances, one for http and one for https. I've been trying to find out why this limitation exists because I know HttpListener can handle registering both for the same listener.

Anyway, if creating two HttpServer instances really doesn't work for you then you will need to look at the Katana project and the Microsoft.Owin.HttpListener. That does support multiple addresses but unfortunately the default Katana startup code doesn't! But I have it on good authority there is a way to customize the HttpListener on startup to make it possible.

查看更多
祖国的老花朵
3楼-- · 2019-04-28 11:38

Because HttpListenerBase (of ServiceStack.Host.HttpListener) does not override its Listener if not-null when starting then the following could be done in your host class which inherits from 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;
}
查看更多
登录 后发表回答