Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get ASP.NET Web API (self-hosted) to listen on *only* localhost?

I'm following the example here for a self-hosted ASP.NET Web API service. However, when specifying "localhost" as the host in the base address, it is translated to "+" (meaning "all available").

var baseAddress = new Uri("http://localhost:13210");
var configuration = new HttpSelfHostConfiguration(baseAddress);
configuration.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "{controller}/{id}",
    defaults: new {id = RouteParameter.Optional});

using (var server = new HttpSelfHostServer(configuration))
{
    server.OpenAsync().Wait();
    stop.WaitOne();
    server.CloseAsync().Wait();
}

I really do want my host bound to just "localhost" -- it will only be accessed from the same machine, and I don't want to mess around with URL ACLs.

How do I configure Web API to not rewrite "localhost" to "+"?

like image 405
Roger Lipscombe Avatar asked Jun 23 '13 12:06

Roger Lipscombe


People also ask

Can ASP NET core Web API be self hosted?

ASP.NET Web API does not require IIS. You can self-host a web API in your own host process. New applications should use OWIN to self-host Web API. See Use OWIN to Self-Host ASP.NET Web API 2.

Can ASP Net Web API ability to both self hosting?

ASP.NET Web API can be either be hosted in IIS or in a separate host process. The former approach is usually appropriate when the Web API is part of a web application and one or more web applications are going to consume it.


Video Answer


1 Answers

Set your HostNameComparisonMode property to Exact:

var config = new HttpSelfHostConfiguration("https://localhost/api/");
config.HostNameComparisonMode = HostNameComparisonMode.Exact;

See this article for more information on HostNameComparisonMode

like image 166
wal Avatar answered Oct 03 '22 09:10

wal