SimpleSecurity.DataProtection 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package SimpleSecurity.DataProtection --version 1.0.0
                    
NuGet\Install-Package SimpleSecurity.DataProtection -Version 1.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="SimpleSecurity.DataProtection" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SimpleSecurity.DataProtection" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="SimpleSecurity.DataProtection" />
                    
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 SimpleSecurity.DataProtection --version 1.0.0
                    
#r "nuget: SimpleSecurity.DataProtection, 1.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 SimpleSecurity.DataProtection@1.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=SimpleSecurity.DataProtection&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=SimpleSecurity.DataProtection&version=1.0.0
                    
Install as a Cake Tool

SimpleSecurity.DataProtection

English | 中文

A drop-in replacement for ASP.NET Core's IDataProtectionProvider that derives all keys from a single secret string — think Laravel's APP_KEY in .env. No key-ring XML files under ~/.aspnet/dataprotection-keys/, no key rotation to synchronize across machines, no shared storage to configure. Set one secret and every instance can read every other instance's protected payloads.

Why

ASP.NET Core's default data protection generates a random key, persists it to an XML key-ring on disk (or Redis / blob storage), and rotates it every 90 days. That's great for a single long-lived host but painful for containers, autoscaled instances, and ephemeral filesystems: lose the key-ring and every cookie / token / antiforgery value becomes undecryptable. This library trades automatic rotation for a fixed, explicitly-managed secret — the same trade-off Laravel makes with APP_KEY.

How it works

  • HKDF-SHA256 derives a distinct AES-256 key from your secret for every purpose (CreateProtector("cookies") and CreateProtector("antiforgery") get different keys — purpose isolation is cryptographic, not cosmetic).
  • AES-GCM provides authenticated encryption; its 128-bit tag detects any tampering, so no separate HMAC is needed.
  • Payload layout: [version:1][nonce:12][tag:16][ciphertext:n]. The version byte is bound as GCM associated data and lets the format evolve without breaking old payloads.

Install

dotnet add package SimpleSecurity.DataProtection

Targets net8.0, net9.0, and net10.0.

Usage

1. Dead simple — inline secret

builder.Services.AddSimpleDataProtection("a-long-random-secret");

2. From an environment variable (the Laravel .env feel)

builder.Services.AddSimpleDataProtection()
    .UseSecretFromEnvironment("APP_KEY");

Fails fast at startup if APP_KEY is missing or empty.

3. From configuration (appsettings.json, user secrets, Key Vault, ...)

builder.Services.AddSimpleDataProtection()
    .UseSecret(builder.Configuration["Security:DataProtectionKey"]!);

4. Fluent / advanced — options delegate and optional salt

builder.Services.AddSimpleDataProtection(options =>
{
    options.UseSecret(builder.Configuration["Security:Key"]!);
    options.UseSalt("my-app-domain-separator"); // optional HKDF salt
});

Every AddSimpleDataProtection(...) overload returns an ISimpleDataProtectionBuilder, so you can keep chaining:

builder.Services
    .AddSimpleDataProtection("secret")
    .UseSalt("tenant-42");

Consuming it

Exactly like the built-in API — inject IDataProtectionProvider and create a protector:

public class TokenService(IDataProtectionProvider provider)
{
    private readonly IDataProtector _protector = provider.CreateProtector("MyApp.Tokens.v1");

    public string Protect(string value) =>
        Convert.ToBase64String(_protector.Protect(Encoding.UTF8.GetBytes(value)));
}

Operational notes

  • Keep the secret secret and stable. Anyone with it can forge and read payloads. Changing it invalidates every existing cookie / token / antiforgery value.
  • Use a high-entropy secret. HKDF stretches the input but cannot add entropy. Prefer 32+ random bytes (e.g. openssl rand -base64 32) over a short passphrase like "abc".
  • Same secret + same salt = interchangeable instances. That is the whole point; it's also why you must distribute the secret to every instance that needs to interoperate.
  • No automatic rotation. To rotate, change the secret (and accept that old payloads stop decrypting), or bump your own purpose string (...Tokens.v2).

License

MIT

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  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
1.0.1 228 6/18/2026
1.0.0 150 6/18/2026