liveBook · Manning

HTTPS redirection

As explained in .NET in Action, Second Edition

HTTPS redirection is the practice of responding to HTTP requests by redirecting clients to the corresponding HTTPS URL.

HTTPS redirection answers an HTTP request with a redirect to the same URL over HTTPS, preventing intercepted traffic from being read or modified. In ASP.NET Core, app.UseHttpsRedirection() commonly produces a temporary 307 response with a Location header containing the HTTPS address. section 9.3

Enable redirection

Add app.UseHttpsRedirection() to the application pipeline. Test it by opening http://localhost:<port>/swagger/index.html and checking the Network tab for a 307 response and HTTPS Location header. section 9.3

Choose the deployment strategy deliberately:

  • Use temporary redirects during development, avoiding browser caching problems caused by permanent redirects. section 9.3
  • In production, avoid exposing an HTTP port behind a proxy such as Istio, or permanently redirect HTTP requests to HTTPS. section 9.3
  • Enable HSTS in production so browsers remember that the site should use HTTPS. section 9.3
Listing 9.3 Enabling HSTS in production
var app = builder.Build();
  
if (app.Environment.IsProduction())   #1
{
    app.UseHsts();                    #2
}

Shows conditional production-only HSTS middleware configuration. section 9.3

Listing 9.4 Configuring HSTS
builder.Services.AddHsts(options =>                       #1
{
  options.Preload = true;                                 #2
  options.IncludeSubDomains = true;                       #3
  options.MaxAge =                                        #4
    DateTime.UtcNow.AddYears(1) - DateTime.UtcNow;        #5
  options.ExcludedHosts.Add("test.manningcatalog.net");   #6
});
  
var app = builder.Build();

Shows HSTS options for preload, subdomains, duration, and excluded hosts. section 9.3

How .NET in Action, Second Edition covers it

How deep it goes
Explains middleware behavior, 307 responses, Location headers, deployment choices, and HSTS configuration.
Examples
Uses Program.cs middleware, a localhost Swagger URL, the .NET development certificate command, and HSTS listings.
What it assumes
Assumes HTTP request handling and teaches why encryption, trusted certificates, and HTTPS enforcement matter.
What you can do after
Apply HTTPS redirection, test its 307 response, and configure production HSTS or proxy termination.
How it connects
Connects HTTP redirection with HTTPS, certificates, reverse proxies, and HTTP Strict Transport Security.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of .NET in Action, Second Edition. The section links point to the parts of the book it draws on.