Couple issues with HTTPS redirect ASP.NET Core

Laimonas Simutis
1 min readOct 12, 2019

It took me some digging to figure out what the issue was so posting here in case someone will run into a similar problem.

It’s a very specific scanario:

  • asp.net core site in a docker container, listening on HTTP port (I was running asp.net core 3.0)
  • you are using an upstream load balancer to do SSL termination (AWS ELB/ALB e.g.).

I ran into two issues.

Problem 1: redirect not kicking in

  1. Make sure you have “app.UseHttpsRedirection();” in your Configure of Startup.cs
  2. If you see “Failed to determine the https port for redirect” in the container output, add this to your ConfigureServices():
    services.AddHttpsRedirection(opt => opt.HttpsPort = 443);

The explicit port requirement might be needed because of this: https://github.com/aspnet/Announcements/issues/301?

Problem 2: infinite redirect

You deploy the site and the browser is spinning in the infinite redirect loop. Here is what I did to get out of it:

  1. In Configure() add app.UseForwardedHeaders() before UseHttpsRedirection() is called.
  2. Add this somewhat interesting configuration in your ConfigureServices():

Interesting, in that it required KnownNetworks and KnownProxies.Clear calls. I found this deep in the docs here: https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/proxy-load-balancer. Without the explicit Clear() call, it continued to do the infinite redirect.

Once these two items were addressed, the system started to happily redirect.

--

--