What is HSTS and why is it in my ASP.NET Core app?
Understand what HSTS is, why ASP.NET Core's UseHsts() middleware enforces HTTPS, and how to configure max-age and preload safely.
When creating a new ASP.NET Core application, you get a set of middleware that performs typical web application duties. Some include serving static assets, routing, HTTPS redirection, and exception handling. Folks looking will also notice a middleware registration of the app.UseHsts() found in every ASP.NET Core app.
What is HSTS, why would you want it, and how do you configure the HSTS options in ASP.NET Core? Let’s find out.
HSTS (HTTP Strict Transport Security) is a header that tells browsers to only communicate with your site over HTTPS, even on the very first request, closing the gap left by HTTP-to-HTTPS redirects. ASP.NET Core’s app.UseHsts() middleware sends this header automatically.
What is HSTS?
HSTS (HTTP Strict Transport Security) is a web security policy, delivered through the Strict-Transport-Security response header, that tells browsers to only connect to your site over HTTPS — never plain HTTP — for a set max-age duration. Once a browser sees the header, it automatically upgrades every future request to that domain to HTTPS, even if a user types http:// or clicks an insecure link.
In more detail, HSTS lets your application server tell clients to use a secure connection when sending requests. As you may know, HTTP is unsecured communication, while HTTPS uses encryption to improve users’ privacy and security. Applications can transition user sessions from HTTP and HTTPS, and historically, it was very common to move from an unsecured part of a website to a secured section. For example, shopping sites used to display store items over HTTP and then only use HTTPS for the checkout process.
This still happens in ASP.NET Core today, but typically only on the first request to the application. You may notice this line somewhere in your ASP.NET Core pipeline definition.
app.UseHttpsRedirection();If a user visits your application using http://, the ASP.NET Core middleware will attempt to rewrite the request to target https://. This is all good and secure, well, except for that first request, and that’s the crucial part. Some unsecured communication still occurs, which can be an issue depending on what is included in that initial request.
With HSTS, we want to communicate several aspects to any client accessing our application:
- Always communicate using HTTPS, no matter what.
- If
includeSubdomainsis present, we also mean all subdomains. - If
preloadis set, the browser will always make HTTPS requests, even on the first request. - The
max-ageof the policy, which can be measured in seconds to years.
The preload value is the most interesting, as browser vendors use it to create a known list of sites that serve only over HTTPS. This allows the browser to switch to HTTPS before the first request. A browser will never communicate with your application over HTTP, even if your server supports it.
While you may be tempted to enable HSTS and the preload functionality immediately, you must be mindful of what your application needs before deploying this feature.
- Serve and maintain a valid certificate
- Redirect from HTTP to HTTPS on the same host
- Serve all subdomains over HTTPS
- Serve an HSTS header with a
max-ageof 1 year,includeSubdomains, and include thepreloadkey. - Redirect responses must also contain the HSTS header.
`Strict-Transport-Security:` `max-age=63072000; includeSubDomains; preload`And finally, you can verify that you’ve satisfied all the requirements by going to https://hstspreload.org.
In summary, HSTS lets you tell browser vendors you want an added level of security by ensuring all communication is encrypted and that requests or responses are never unsecured. In short, enforce HTTPS.
HSTS in ASP.NET Core
Let’s get back to ASP.NET Core. You’ve likely seen this line in your applications but never clicked through the link in the comment.
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.app.UseHsts();Why is the default value 30 days, and why should you consider changing it for production scenarios?
30 days is a nice round number, just long enough to be helpful but not so long that if you make a mistake, it will take forever for browser vendors to resolve. That said, you should change the value for production. The HSTS preload guidance is a gradual smoke test and will likely take a few application deployments to work through.
Start with a low max-age of 5 minutes, test your site, then graduate to 1 week and to 1 month.
max-age=2592000; includeSubDomainsAlong the way, test your site for errors or issues related to HTTPS traffic. Common HTTP-to-HTTPS issues include static assets and CDN dependencies. When you’re confident you’ve squashed all issues, you can add the preload value and increase max-age to 2 years.
You’ll need to configure the HstsOptions object registered in your ASP.NET Core services collection to work up to this.
builder.Services.Configure<HstsOptions>(o =>{ o.Preload = true; o.MaxAge = new TimeSpan(730 /* 2 years */); o.IncludeSubDomains = true;});The next time a major browser provider (Google, Mozilla, or Microsoft) crawls your site, it’ll be added to the preload list hard-coded into browsers.
If you’re running a multi-tenant site and need to exclude different tenants from a particular policy, you can also set the ExcludeHosts property.
builder.Services.Configure<HstsOptions>(o =>{ o.Preload = true; o.MaxAge = new TimeSpan(730 /* 2 years */); o.IncludeSubDomains = true; o.ExcludedHosts.Add("example.com"); o.ExcludedHosts.Add("www.example.com");});Note that if you exclude a subdomain from a top-level domain that opts into HSTS, you can’t get preload because you haven’t met the requirements.
Should I Turn on HSTS?
HSTS adds a layer of security that can prevent man-in-the-middle attacks from protocol downgrades and cookie hijacking. That said, ASP.NET Core has mechanisms to secure cookies, such as Data Protection encryption and browser hints like Secure, HttpOnly, and SameSite. As long as you don’t change those values, you’ll be following best practices. That said, always consult a security professional and run security audits periodically.
HSTS can be annoying if you accidentally enable it in a development environment without a certificate, but in most production environments, you should have one.
In general, yes, you should enable it, but be mindful when and where you enable it, as it can be challenging to get browsers to refresh the policy.
Conclusion
You now know a little bit more about HSTS, why it’s included in your ASP.NET Core application, and how and when to configure it. HSTS adds an extra layer of security by ensuring all communication stays over HTTPS, including initial requests. That said, you’ll need to do the work to enable preload and plan for weeks of deployments, testing, and audits to make sure everything works smoothly.
As always, thanks for reading, and cheers.
FAQ
What is HSTS?
HSTS (HTTP Strict Transport Security) is a security header that forces browsers to use HTTPS for every request to your site, including the first one. After a browser receives the Strict-Transport-Security header once, it refuses to send any plain-HTTP request to that domain until the header’s max-age expires, protecting users from protocol-downgrade and man-in-the-middle attacks.
What does UseHsts() do?
It adds the Strict-Transport-Security response header to your ASP.NET Core app, instructing browsers to only connect over HTTPS for the configured max-age duration, instead of allowing a plain HTTP request to go through first.
Should I enable HSTS in development?
No. The ASP.NET Core templates only call app.UseHsts() outside the Development environment, since local dev usually lacks a trusted certificate and a browser that’s remembered HSTS for localhost can make testing painful.
HSTS vs HTTPS redirection — what’s the difference?
UseHttpsRedirection() responds to an incoming HTTP request by redirecting it to HTTPS, meaning the first request is still sent unencrypted. HSTS tells the browser to never issue an HTTP request to your domain in the first place, closing that initial gap entirely.
For related security topics, see security scanning your .NET Core applications and adding headers to a response in ASP.NET 5.
References
Around the web
Comments
Loading comments from GitHub Discussions…