Audacia.SecureHeadersMiddleware 4.0.0

dotnet add package Audacia.SecureHeadersMiddleware --version 4.0.0
                    
NuGet\Install-Package Audacia.SecureHeadersMiddleware -Version 4.0.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="Audacia.SecureHeadersMiddleware" Version="4.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Audacia.SecureHeadersMiddleware" Version="4.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Audacia.SecureHeadersMiddleware" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Audacia.SecureHeadersMiddleware --version 4.0.0
                    
#r "nuget: Audacia.SecureHeadersMiddleware, 4.0.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.
#:package Audacia.SecureHeadersMiddleware@4.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Audacia.SecureHeadersMiddleware&version=4.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Audacia.SecureHeadersMiddleware&version=4.0.0
                    
Install as a Cake Tool

Overview

Audacia.SecureHeadersMiddleware is a wrapper around the NetEscapades.AspNetCore.SecurityHeaders library. It uses middleware to add security-related headers, such as a Content Security Policy.

Usage

There are two IApplicationBuilder extension methods to add the recommended security headers; the one to use depends on the type of project:

  • UseApiSecurityHeaders for API projects
  • UseIdentitySecurityHeaders for MVC projects that implement an authentication provider such as IdentityServer or OpenIddict

Both methods return a HeaderPolicyCollection, which can be used to customize the headers. For example, suppose you want to use the default API headers, and also add a bespoke header called My-Security-Header, this can be achieved as follows:

app.UseApiSecurityHeaders().AddCustomHeader("My-Security-Header", "header value");

Exclude caching for some endpoints

ASP.NET Core includes a [ResponseCache] attribute that configures caching headers at the controller or action level. This will not be respected when using app.UseApiSecurityHeaders().

To make the middleware respect this attribute, the UseApiSecurityHeaders middleware can be conditionally applied with app.UseWhen().

  • UseApiSecurityHeaders can be used for endpoints without a [ResponseCache] attribute
  • AddDefaultSecurityHeaders from NetEscapades.AspNetCore.SecurityHeaders can be used for endpoints with a [ResponseCache] attribute
/// <summary>
/// Use middleware to add security headers with custom support for the <see cref="ResponseCacheAttribute"/>.
/// </summary>
internal static IApplicationBuilder UseCustomSecurityHeaders(this IApplicationBuilder app)
{
    // When the endpoint has a ResponseCacheAttribute, only apply the default headers
    app.UseWhen(
        ctx => ctx.IsResponseCachedEndpoint(),
        builder =>
        {
            var headerPolicyCollection = new HeaderPolicyCollection().AddDefaultSecurityHeaders();
            builder.UseSecurityHeaders(headerPolicyCollection);
        });

    // For all other endpoints, apply the Audacia API headers which include cache prevention
    app.UseWhen(
        ctx => !ctx.IsResponseCachedEndpoint(),
        builder => builder.UseApiSecurityHeaders());

    return app;
}

/// <summary>
/// Checks if the endpoint in the context has the <see cref="ResponseCacheAttribute"/>.
/// </summary>
private static bool IsResponseCachedEndpoint(this HttpContext context)
{
    var endpoint = context.GetEndpoint();

    var filter = endpoint?.Metadata.GetMetadata<ResponseCacheAttribute>();

    return filter is not null;
}

...

// Must call this after app.UseRouting()
app.UseCustomSecurityHeaders();

Contributing

We welcome contributions! Please feel free to check our Contribution Guidlines for feature requests, issue reporting and guidelines.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
4.0.0 157 5/11/2026
3.1.3 1,164 1/5/2026