SafeWebCore 1.9.0

dotnet add package SafeWebCore --version 1.9.0
                    
NuGet\Install-Package SafeWebCore -Version 1.9.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="SafeWebCore" Version="1.9.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SafeWebCore" Version="1.9.0" />
                    
Directory.Packages.props
<PackageReference Include="SafeWebCore" />
                    
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 SafeWebCore --version 1.9.0
                    
#r "nuget: SafeWebCore, 1.9.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 SafeWebCore@1.9.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=SafeWebCore&version=1.9.0
                    
Install as a Cake Addin
#tool nuget:?package=SafeWebCore&version=1.9.0
                    
Install as a Cake Tool

๐Ÿ›ก๏ธ SafeWebCore

A lightweight, high-performance .NET 10 and .NET 11 middleware library that adds security headers to your ASP.NET Core applications. Targets an A+ rating on securityheaders.com out of the box.

Current version: 1.9.0

New in 1.9.0 (multi-targeting release โ€” no public API, default, preset or configuration change):

  • lib/net11.0 next to lib/net10.0 โ€” a .NET 11 application now resolves a build compiled against .NET 11 (C# 15 and the .NET 11 JIT) instead of falling back to the net10.0 asset. The net10.0 build is unchanged.
  • C# 15 collection-expression arguments โ€” [with(capacity: โ€ฆ)] / [with(comparer: โ€ฆ)] where the capacity or comparer is known, so the diagnostics preview no longer grows its header map. Every emitted header value is identical.
  • Companion packages โ€” SafeWebCore.FraudDetection moves to 1.2.0, SafeWebCore.JwtBearer to 1.1.0 (its Microsoft.AspNetCore.Authentication.JwtBearer dependency is now per target: 10.0.12 on net10.0, 11.0.0 on net11.0), and SafeWebCore.Testing to 1.0.0-preview.3.
  • Repository โ€” keyless publishing through nuget.org trusted publishing, and a mutation-testing baseline that records what the test suite actually catches.

Backward Compatibility Goal

SafeWebCore keeps a strict 100% backward compatibility contract. New capabilities are additive and opt-in, so existing configurations keep their current behavior.

Two Ways to Use SafeWebCore

Option 1 โ€” Strict A+ Preset (fastest)

One line for the strictest A+ configuration. Defined in ServiceCollectionExtensions.AddNetSecureHeadersStrictAPlus().

using SafeWebCore.Extensions;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddNetSecureHeadersStrictAPlus();

var app = builder.Build();
app.UseNetSecureHeaders();
app.Run();

Customize the preset โ€” CSP directives are space-separated, add multiple origins in one string:

builder.Services.AddNetSecureHeadersStrictAPlus(opts =>
{
    // Single origin
    opts.Csp = opts.Csp with { ImgSrc = "'self' https://cdn.example.com" };

    // Multiple origins โ€” just separate with spaces
    opts.Csp = opts.Csp with { ImgSrc = "'self' https://cdn1.example.com https://cdn2.example.com data:" };

    // Multiple directives at once
    opts.Csp = opts.Csp with
    {
        ConnectSrc = "'self' https://api.example.com wss://ws.example.com",
        FontSrc = "'self' https://fonts.gstatic.com https://cdn.example.com"
    };

    // Non-CSP headers
    opts.ReferrerPolicyValue = "strict-origin-when-cross-origin";
});

Option 2 โ€” Fully Custom Configuration

Full control over every header via ServiceCollectionExtensions.AddNetSecureHeaders():

using SafeWebCore.Builder;
using SafeWebCore.Extensions;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddNetSecureHeaders(opts =>
{
    // Transport security
    opts.EnableHsts = true;
    opts.HstsValue = "max-age=31536000; includeSubDomains";

    // Framing
    opts.EnableXFrameOptions = true;
    opts.XFrameOptionsValue = "SAMEORIGIN";

    // MIME sniffing
    opts.EnableXContentTypeOptions = true;
    opts.XContentTypeOptionsValue = "nosniff";

    // Referrer
    opts.EnableReferrerPolicy = true;
    opts.ReferrerPolicyValue = "strict-origin-when-cross-origin";

    // Permissions
    opts.EnablePermissionsPolicy = true;
    opts.PermissionsPolicyValue = "camera=(), microphone=(), geolocation=()";

    // Cross-Origin isolation
    opts.EnableCoep = true;
    opts.CoepValue = "require-corp";
    opts.EnableCoop = true;
    opts.CoopValue = "same-origin";
    opts.EnableCorp = true;
    opts.CorpValue = "same-origin";

    // Server header
    opts.RemoveServerHeader = true;

    // CSP โ€” use the fluent builder
    opts.Csp = new CspBuilder()
        .DefaultSrc("'none'")
        .ScriptSrc("'nonce-{nonce}' 'strict-dynamic' https:")
        .StyleSrc("'nonce-{nonce}'")
        .ImgSrc("'self' https: data:")
        .FontSrc("'self' https://fonts.gstatic.com")
        .ConnectSrc("'self' wss://realtime.example.com")
        .FrameAncestors("'none'")
        .BaseUri("'none'")
        .FormAction("'self'")
        .UpgradeInsecureRequests()
        .Build();
});

var app = builder.Build();
app.UseNetSecureHeaders();
app.Run();

Both methods are defined in SafeWebCore.Extensions.ServiceCollectionExtensions.

Strict A+ Headers

Header Strict A+ Value
Strict-Transport-Security max-age=63072000; includeSubDomains; preload
Content-Security-Policy Nonce-based, strict-dynamic, Trusted Types
X-Frame-Options DENY
X-Content-Type-Options nosniff
Referrer-Policy no-referrer
Permissions-Policy All recognized features denied (scanner-safe, modern Chromium tokens only)
Cross-Origin-Embedder-Policy require-corp
Cross-Origin-Opener-Policy same-origin
Cross-Origin-Resource-Policy same-origin
Server (removed)
X-Powered-By (removed)

Features

  • ๐Ÿ”’ Strict A+ preset โ€” one-line setup with the strictest security headers
  • ๐ŸŒ Browser-safe Permissions-Policy โ€” preset emits only scanner-recognised tokens; invalid directives (e.g. identity-credentials-get, otp-credentials, publickey-credentials-create, window-management) and stale tokens removed to pass securityheaders.com checks without warnings
  • ๐Ÿ› ๏ธ Fully custom โ€” configure every header and CSP directive individually
  • ๐Ÿงฉ Nonce-based CSP โ€” per-request cryptographic nonces for scripts and styles
  • ๐Ÿงท Razor nonce TagHelpers โ€” auto-add nonce to <script> and <style> in Razor views
  • ๐Ÿ›ฃ๏ธ Path-based policies โ€” assign different security profiles per route prefix (longest-prefix wins)
  • ๐Ÿงช Startup validation โ€” fail fast on invalid combinations and duplicate path policies
  • ๐Ÿ“ CSP Report-Only mode โ€” safely test policy changes before hard enforcement
  • ๐Ÿงฑ Typed policy builders โ€” strongly typed builders for Referrer-Policy, Permissions-Policy, and COEP/COOP/CORP
  • ๐Ÿงญ First-class upcoming header support โ€” configure non-standard or emerging headers through AdditionalHeaders (opt-in)
  • ๐Ÿ“ก First-class Reporting API endpoint support โ€” emit Reporting-Endpoints from typed ReportingEndpoints options (opt-in)
  • ๐Ÿ“‹ Full CSP Level 3 (W3C Recommendation) โ€” all 22 directives, nonce/hash support, strict-dynamic, report-to, worker-src, frame-src, manifest-src, script-src-elem/attr, style-src-elem/attr
  • ๐Ÿ”ฎ CSP Level 4 ready โ€” Trusted Types (require-trusted-types-for, trusted-types), fenced-frame-src (Privacy Sandbox)
  • ๐ŸŽฏ Fluent CSP Builder โ€” type-safe, chainable API with full XML documentation
  • โšก Zero-allocation nonce generation โ€” stackalloc + RandomNumberGenerator, plus TryWriteNonce(Span<char>) for fully heap-free scenarios (v1.1.0)
  • ๐Ÿ” HttpContext.GetCspNonce() โ€” discoverable extension method to retrieve the per-request nonce (v1.1.0)
  • ๐Ÿš€ Pre-built CSP template โ€” CSP header string computed once at startup, not per-request (v1.1.0)
  • ๐Ÿ”Œ Extensible โ€” custom IHeaderPolicy implementations
  • ๐Ÿ“Š CSP violation reporting โ€” built-in /csp-report endpoint using Reporting API v1

First-class Upcoming Headers (Opt-in)

Use AdditionalHeaders when you want to emit upcoming or non-standard headers without writing a custom policy type:

builder.Services.AddNetSecureHeaders(opts =>
{
    opts.AdditionalHeaders.Add(new()
    {
        Name = "Document-Policy",
        Value = "force-load-at-top"
    });
});

First-class Reporting Endpoints (Opt-in)

Use ReportingEndpoints to emit the Reporting-Endpoints response header and map endpoint groups used by CSP report-to:

builder.Services.AddNetSecureHeaders(opts =>
{
    opts.Csp = opts.Csp with { ReportTo = "default" };

    opts.ReportingEndpoints.Add(new()
    {
        Group = "default",
        Url = "https://reports.example.com/csp"
    });
});

Emitted header value:

Reporting-Endpoints: default="https://reports.example.com/csp"

Validate Your Headers

After deploying, test your security headers with:

  • securityheaders.com โ€” Grades all response headers A+ through F. With the Strict A+ preset you should score A+ immediately.
  • Google CSP Evaluator โ€” Paste your Content-Security-Policy value to check for misconfigurations (missing object-src, 'unsafe-inline' without nonce, missing 'strict-dynamic', etc.).

Documentation

Full documentation: github.com/MPCoreDeveloper/SafeWebCore/docs

Planning documents:

License

MIT โ€” see LICENSE

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.  net11.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.
  • net11.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on SafeWebCore:

Package Downloads
SafeWebCore.Testing

Testing helpers for SafeWebCore. Includes header assertions, CSP/nonce assertions, and bootstrap helpers for integration tests.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.9.0 142 11/15/2026
1.8.1 1,933 9/29/2026
1.8.0 121 9/28/2026
1.7.0 2,766 8/4/2026
1.6.0 263 7/25/2026
1.3.5 255 6/27/2026
1.3.0 340 5/2/2026
1.2.0 659 4/2/2026
1.1.0 157 3/29/2026
1.0.0 237 3/28/2026

## v1.9.0 โ€” Multi-targeting .NET 10 and .NET 11

**100% backwards compatible** โ€” the public API surface, defaults, presets and configuration paths are unchanged. The package now ships a `net11.0` asset next to the existing `net10.0` one, so a .NET 11 application resolves the build made for its runtime instead of falling back to `net10.0`.

**What's new:**
- `lib/net11.0` โ€” the same public API, compiled against .NET 11 (C# 15 and the .NET 11 JIT). `lib/net10.0` is unchanged and stays the target for .NET 10 applications.
- C# 15 collection-expression arguments (`[with(capacity: ...)]`, `[with(comparer: ...)]`) where the capacity or comparer is known, so the diagnostics preview no longer grows its header map and the options validator passes its comparer inside the collection expression. No behavior change.
- Repository: publishing is keyless through nuget.org trusted publishing, and mutation testing with Stryker.NET records the test-suite baseline in `docs/development/mutation-testing.md`.

See CHANGELOG.md and docs/ for full details.