liveBook · Manning

Hsts header

As explained in Building Web APIs with ASP.NET Core

An HTTP security policy that instructs browsers to access a site only over HTTPS.

HSTS is an HTTP security policy that tells browsers to access a web server only over HTTPS, keeping connections on secure channels. section 12.1.4

Treat HSTS as a low-cost production security improvement: it needs little configuration, has negligible performance impact, and supports security assessments. section 12.1.4

Enable HSTS outside development

In the web API's Program.cs pipeline, keep development tooling in the development branch and call app.UseHsts() in the production branch. section 12.1.4

Listing 12.1 Program.cs file: HTTP security headers
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}
else
{
    // HTTP Security Headers
    app.UseHsts();                                                   #1
    app.Use(async (context, next) =>
    {
        context.Response.Headers.Add("X-Frame-Options",
            "sameorigin");                                           #2
        context.Response.Headers.Add("X-XSS-Protection",
            "1; mode=block");                                        #3
        context.Response.Headers.Add("X-Content-Type-Options",
            "nosniff");                                              #4
        context.Response.Headers.Add("Content-Security-Policy",
            "default-src ' self' ;");                                #5
        context.Response.Headers.Add("Referrer-Policy",
            "strict-origin");                                        #6
        await next();
    });
}

Inspect the Program.cs listing alongside related security-header middleware. section 12.1.4

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Defines the browser policy and shows its middleware call, but does not detail header directives or attack mechanics.
Examples
Uses an environment-conditional Program.cs pipeline containing app.UseHsts() in the non-development branch.
What you can do after
Operate/configure HSTS by enabling app.UseHsts() for the production web application pipeline.
How it connects
Connects HSTS with HTTPS, browsers, web APIs, and other HTTP security headers.

Also explained in

Compare how 5 books explain hsts header →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Web APIs with ASP.NET Core. The section links point to the parts of the book it draws on.