Joonasw.AspNetCore.SecurityHeaders 2.7.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Joonasw.AspNetCore.SecurityHeaders --version 2.7.0
NuGet\Install-Package Joonasw.AspNetCore.SecurityHeaders -Version 2.7.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Joonasw.AspNetCore.SecurityHeaders" Version="2.7.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Joonasw.AspNetCore.SecurityHeaders --version 2.7.0
#r "nuget: Joonasw.AspNetCore.SecurityHeaders, 2.7.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. 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=2.7.0

// Install Joonasw.AspNetCore.SecurityHeaders as a Cake Tool
#tool nuget:?package=Joonasw.AspNetCore.SecurityHeaders&version=2.7.0

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.UseHsts(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 Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp1.0 was computed.  netcoreapp1.1 was computed.  netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard1.5 is compatible.  netstandard1.6 was computed.  netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen30 was computed.  tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (5)

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

Package Downloads

IIR's U.S. Web Design System (USWDS) Components for .NET 6 and Razor Pages


A collection of middleware and methods for helping secure the headers for websites in .NET 6.


Package Description


Addons permissions-policy to aspnetcore-security-headers library.


Easy to use and simple ASP.NET Core services for hardening and securing your web applications or APIs. Examples and Github repo coming soon.

GitHub repositories (1)

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

Repository Stars
Exceptionless application
Version Downloads Last updated
5.0.0 261,103 9/13/2023
4.0.1 623,996 3/3/2022
3.0.0 978,887 10/24/2019
2.9.0 96,604 6/21/2019
2.8.1 99,095 1/1/2019
2.8.0 677 1/1/2019
2.7.0 73,813 8/11/2018
2.6.0 34,950 5/27/2018
2.5.1 4,362 5/1/2018
2.5.0 2,933 4/14/2018
2.4.1 1,083 4/14/2018
2.4.0 22,205 1/21/2018
2.3.0 2,014 1/6/2018
2.2.0 8,773 11/20/2017
2.1.0 7,716 10/29/2017
2.0.0 2,970 9/30/2017
1.1.2 14,827 7/14/2017
1.1.0 1,712 5/17/2017
1.0.1 3,680 1/23/2017
1.0.0 5,064 1/22/2017

Adds support for prefetch-src in CSP.
Adds Expect-CT, Feature-Policy, Referrer-Policy, X-Content-Type-Options, X-Frame-Options and X-XSS-Protection headers.