Member-only story
Couple issues with HTTPS redirect ASP.NET Core
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
- Make sure you have “app.UseHttpsRedirection();” in your Configure of Startup.cs
- 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:
- In Configure() add app.UseForwardedHeaders() before UseHttpsRedirection() is called.
- 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.