Joonasw.AspNetCore.SecurityHeaders 4.0.1

.NET 5.0 .NET Core 3.1
Install-Package Joonasw.AspNetCore.SecurityHeaders -Version 4.0.1
dotnet add package Joonasw.AspNetCore.SecurityHeaders --version 4.0.1
<PackageReference Include="Joonasw.AspNetCore.SecurityHeaders" Version="4.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Joonasw.AspNetCore.SecurityHeaders --version 4.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Joonasw.AspNetCore.SecurityHeaders, 4.0.1"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Joonasw.AspNetCore.SecurityHeaders as a Cake Addin
#addin nuget:?package=Joonasw.AspNetCore.SecurityHeaders&version=4.0.1

// Install Joonasw.AspNetCore.SecurityHeaders as a Cake Tool
#tool nuget:?package=Joonasw.AspNetCore.SecurityHeaders&version=4.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Add CSP, HSTS or HPKP headers to an ASP.NET Core app

This library allows you to add Content Security Policy, Strict Transport Security and Public Key Pin headers via middleware.

You can get the library from NuGet:

Example configuration

// Enable Strict Transport Security with a 30-day caching period
// Do not include subdomains
// Do not allow preload
app.UseStrictTransportSecurity(new HstsOptions(TimeSpan.FromDays(30), includeSubDomains: false, preload: false));

// Use certificate pinning with:
// - 30-day caching period
// - One pin in SHA-256 form
// - Report-Only = Invalid certificate should not be reported, but:
// - Report problems to /hpkp-report
app.UseHpkp(hpkp =>
    hpkp.UseMaxAgeSeconds(30 * 24 * 60 * 60)

// Content Security Policy
app.UseCsp(csp =>
    // If nothing is mentioned for a resource class, allow from this domain

    // Allow JavaScript from:
        .FromSelf() //This domain
        .From("localhost:1591") //These two domains

    // CSS allowed from:


    // HTML5 audio and video elemented sources can be from:

    // Contained iframes can be sourced from:
        .FromNowhere(); //Nowhere, no iframes allowed

    // Allow AJAX, WebSocket and EventSource connections to:

    // Allow fonts to be downloaded from:

    // Allow object, embed, and applet sources from:

    // Allow other sites to put this in an iframe?
        .FromNowhere(); // Block framing on other sites, equivalent to X-Frame-Options: DENY

    // Do not block violations, only report
    // This is a good idea while testing your CSP
    // Remove it when you know everything will work
    // Where should the violation reports be sent to?

    // Do not include the CSP header for requests to the /api endpoints
    csp.OnSendingHeader = context =>
        context.ShouldNotSend = context.HttpContext.Request.Path.StartsWithSegments("/api");
        return Task.CompletedTask;

Content Security Policy can be quite daunting. Here is a nice page to find out what the options do:

For violation reports, I recommend using Scott Helme's Report URI service at


CSP allows you to also specify a nonce value, which makes it easier to have inline script and style elements like this on a page:

    h1 {
      color: red;

To allow them without nonces, you might have to use the unsafe-inline option.

Instead of doing that, we can add the following service in Startup:

public void ConfigureServices(IServiceCollection services)
    // ... other service registrations

    // Add services necessary for nonces in CSP, 32-byte nonces
    services.AddCsp(nonceByteAmount: 32);

Then you need to modify your CSP definition to include the nonce:

    .AddNonce(); //<----

    .AddNonce(); //<-----

Then to use the nonce tag helper, we need to import it in _ViewImports.cshtml:

@addTagHelper *, Joonasw.AspNetCore.SecurityHeaders

Then we just need to use it in the Razor view:

  <script asp-add-nonce="true">
  <style asp-add-nonce="true">
    h1 {
      color: red;

Now a unique nonce is generated every request and inserted into the CSP header + the elements you want.

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp3.1
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Joonasw.AspNetCore.SecurityHeaders:

Package Downloads

Package Description

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on Joonasw.AspNetCore.SecurityHeaders:

Repository Stars
Exceptionless server and jobs
Version Downloads Last updated
4.0.1 44,846 3/3/2022
3.0.0 551,413 10/24/2019
2.9.0 87,512 6/21/2019
2.8.1 80,965 1/1/2019
2.8.0 508 1/1/2019
2.7.0 57,920 8/11/2018
2.6.0 30,041 5/27/2018
2.5.1 4,066 5/1/2018
2.5.0 2,614 4/14/2018
2.4.1 857 4/14/2018
2.4.0 21,176 1/21/2018
2.3.0 1,721 1/6/2018
2.2.0 6,509 11/20/2017
2.1.0 6,981 10/29/2017
2.0.0 2,621 9/30/2017
1.1.2 14,473 7/14/2017
1.1.0 1,474 5/17/2017
1.0.1 3,249 1/23/2017
1.0.0 3,952 1/22/2017

Rename UseHsts to UseStrictTransportSecurity to remove conflict with framework counterpart.