Andy S
Andy S

Reputation: 8861

Problems with ASP.NET Core site using http.sys and Microsoft Edge

I'm having a ton of problems getting an ASP.NET Core 2.1 web application up and running. I need it to run under http.sys (WebListener) on a shared port (80 or 443). I'd also like it to automatically redirect from http (80) to https (443). Of course, I don't want to hard code the listener addresses for http.sys - I need to pull those from a configuration file, but they're hard coded for now. I reserved the appropriate URLs with netsh, but when I run the app I get a warning:

warn: Microsoft.AspNetCore.Server.HttpSys.MessagePump[0]
      Overriding address(es) 'http://sharedhost.vbcoa.com:80/app/, https://sharedhost.vbcoa.com:443/app/'. Binding to endpoints added to UrlPrefixes instead.

The app starts, but I can't browse to it with Microsoft Edge at all. Any other web browser is fine - as long as I disable HTTPS. For some reason, the application is forwarding to port 5001, instead of 443.

Upvotes: 1

Views: 2149

Answers (1)

Andy S
Andy S

Reputation: 8861

I figured all of this out. There are four problems. I'll address them each individually.

  • When configuring http.sys, a warning is issued about overriding local URLs

The UseHttpSys extension method of IWebHostBuilder accepts an options argument with a UrlPrefixes property. However, this is not where you should configure URLs - even if you're using http.sys. You can hardcode them with the UseUrls extension method of IWebHostBuilder, but it would be better to pull it from configuration, which leads to the second problem:

  • Configuration should be read from appsettings.json

To specify which URLs you want to run the application on, add them to the "urls" element in appsettings.json, as follows:

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*",
  "urls": "http://sharedhost.vbcoa.com:80/app/;https://sharedhost.vbcoa.com:443/app/"
}

Then you'll need to create a ConfigurationBuilder object, add the appsettings.json file to it, build the configuration (with the Build method) and tell IWebHostBuilder to use that configuration, with the UseConfiguration extension method:

public static void Main(string[] args)
{
    var configBuilder = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json");

    var hostBuilder = WebHost.CreateDefaultBuilder(args)
        .UseConfiguration(configBuilder.Build())
        .UseHttpSys()
        .UseStartup<Startup>();

    hostBuilder.Build().Run();
}
  • Redirection goes to port 5001, not 443

HTTPS redirection is specified in the Configure method of Startup - that functionality comes out of the box. However, by default it will forward to port 5001, even if you have another port specified in your bound URLs from above. To override it, you need to inject HTTPS redirection options via a service. That's handled in the ConfigureServices method of Startup. Add the following line to that method:

services.AddHttpsRedirection(options => { options.HttpsPort = 443; });
  • Microsoft Edge won't show the web app, even though every other browser will

This is a problem with localhost loopback isolation in Windows Store apps. It seems to affect Windows 10 Enterprise, as discussed here: Microsoft Edge is not able to recognize localhost. To correct it, you need to do two things:

  1. Make sure "Allow localhost loopback" is checked in Edge's "about:flags" page.
  2. Launch a Command Prompt or Powershell Prompt as an Administrator and enter the following:

    CheckNetIsolation LoopbackExempt -a -n=Microsoft.MicrosoftEdge_8wekyb3d8bbwe

That should do it!

Upvotes: 5

Related Questions