ASP.NET Core MVC Intermediate

Enforcing HTTPS

Every page of a modern site should be served over HTTPS, so data between the browser and your server is encrypted. This lesson shows how ASP.NET Core redirects HTTP to HTTPS, what HSTS adds, and how the development certificate works.

From HTTP to HTTPS, then HTTPS only
  1. StartBrowser requests http://The first visit, typed without https
  2. Step 2HTTPS redirectionUseHttpsRedirection answers with a redirect to https://
  3. Step 3Secure requestThe browser repeats the request over HTTPS
  4. Step 4HSTS header sentStrict-Transport-Security: max-age=...
  5. EndBrowser remembersFuture visits go straight to HTTPS, even if the user types http

The idea in plain words

HTTPS encrypts traffic and proves to the browser that it is talking to your server. ASP.NET Core gives you two tools. UseHttpsRedirection sends any plain HTTP request to the HTTPS address. UseHsts adds the HSTS header, which tells browsers to use HTTPS for your site for a period of time without even trying HTTP first.

HSTS is sticky, so the template turns it on only outside Development. Locally, the .NET SDK provides a development certificate, trusted with dotnet dev-certs https --trust.

Example

C#
builder.Services.AddHsts(options =>
{
    options.MaxAge = TimeSpan.FromDays(365);
    options.IncludeSubDomains = true;
});

var app = builder.Build();

if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    app.UseHsts();
}
app.UseHttpsRedirection();

Behind a proxy or load balancer

When TLS ends at a proxy (Nginx, IIS, Azure Front Door, a load balancer), the app receives plain HTTP from the proxy. Use the forwarded headers middleware so the app knows the original request was HTTPS; otherwise redirects can loop.

C#
builder.Services.Configure<ForwardedHeadersOptions>(options =>
    options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto);

var app = builder.Build();
app.UseForwardedHeaders(); // before other middleware

Common mistakes

  • Enabling HSTS while testing on localhost, so the browser refuses HTTP for localhost for a long time.
  • Relying on redirection for APIs; API clients may send data over HTTP before being redirected. Configure APIs to listen only on HTTPS.
  • Forgetting forwarded headers behind a proxy, causing redirect loops or wrong links.

Interview question

What is HSTS and why is it not enabled in Development?

HTTP Strict Transport Security is a response header that tells the browser to contact the site only over HTTPS for the given max-age, upgrading future http links automatically and preventing SSL stripping attacks. It is cached by the browser per host, so enabling it on localhost would force HTTPS for every local app on that host name for a long time, which is why the template only calls UseHsts outside Development.

Your progress is saved only in this browser.

Want the full details? Read the official Microsoft documentation for this topic.