liveBook · Manning

HttpsRedirectionMiddleware

As explained in .NET in Action, Second Edition

UseHttpsRedirection is ASP.NET Core middleware configuration that redirects HTTP requests to HTTPS URLs.

UseHttpsRedirection configures ASP.NET Core middleware that answers HTTP requests with a redirect to the same URL over HTTPS. HTTPS protects request and response contents from interception or tampering and requires a certificate. section 9.3

Basic configuration and verification

Add app.UseHttpsRedirection(); to Program.cs. Requesting http://localhost:<port>/swagger/index.html should return status 307 with a Location header containing the HTTPS URL. The .NET CLI can install a trusted development certificate with dotnet dev-certs https --trust. section 9.3

Choose the deployment strategy deliberately:

  • Avoid exposing an HTTP port by placing the service behind a proxy that terminates HTTPS and manages the certificate. section 9.3
  • Use HSTS with redirection when browsers should treat the site as HTTPS-only; enable HSTS in production rather than development. section 9.3
Listing 9.3 Enabling HSTS in production
var app = builder.Build();
  
if (app.Environment.IsProduction())   #1
{
    app.UseHsts();                    #2
}

Shows a production-only UseHsts configuration that complements HTTPS redirection. section 9.3

How .NET in Action, Second Edition covers it

How deep it goes
Explains configuration, 307 behavior, certificates, and production alternatives, but defers broader HTTPS defaults.
Examples
Uses Program.cs, Swagger’s localhost URL, a 307 response, a Location header, and the dotnet dev-certs command.
What it assumes
Assumes familiarity with ASP.NET Core middleware and HTTP, while explaining certificates and redirect status codes.
What you can do after
Operate/configure HTTPS redirection and verify its redirect response during development.
How it connects
Connects HTTPS redirection with TLS certificates, HTTP status codes, reverse proxies, and HSTS.

Also explained in

Compare how 2 books explain HttpsRedirectionMiddleware →

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.