Search code examples
c#asp.net-web-apiowinself-hosting

Web API 2 OWIN Self Host can only connect via IP address


I've created a self hosted service using the Microsoft OWIN 3.0.1, and Web API 5.2.3 NuGet packages. It is also using the Microsoft.AspNet.WebApi Cors.5.2.3 and Microsoft.Owin.Cors 3.0.1 packages for CORS support. The CORS setup is configured as follows:

private static void ConfigureCrossOriginResourceSharing(IAppBuilder app, HttpConfiguration config)
{
    app.UseCors(CorsOptions.AllowAll);

    var cors = new EnableCorsAttribute("*", "*", "*");
    config.EnableCors(cors);
}

The hosting is started as follows:

var startOptions = new StartOptions(url: "http://*:64000/");

Task.Run(() =>
{
    // Start OWIN host 
    using (WebApp.Start(startOptions, startUp.Configuration))
    {
        //etc.

When connecting to the service I can only use an IP address to call it (e.g. http://192.168.0.1:64000/). When using the DNS name (i.e. http://hostname:64000/) an HTTP 502 Bad Gateway error is returned. This isn't ideal.

I assume this has something to do with the CORS configuration. Any ideas what could be wrong?

UPDATE: At the suggestion below I've changed the start url to http://+:64000/. I am now getting a 400 Bad Request response from the client. I can ping the server which resolves to a fully qualified name ( e.g. hostname.domain.corp domain suffix) which I've also tried but get the same result.


Solution

  • This turned out to be related to the authentication setup for the self hosted site. For some reason on our network (possibly 2003 AD vs 2012 servers doing the hosting, I'm unsure) the Kereberos authentication was failing. When using IP addresses the authentication fell back to using NTLM which succeeded. Setting the authentication to only use NTLM allows both host names and IP addresses to be used.

    Before (failed, IP only as it fell back to NTLM):

    listener.AuthenticationSchemes = AuthenticationSchemes.IntegratedWindowsAuthentication;
    

    After (success for both host and IP):

    listener.AuthenticationSchemes = AuthenticationSchemes.Ntlm;