SimpleSecurity.DataProtection
1.0.0
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
<PackageReference Include="SimpleSecurity.DataProtection" Version="1.0.0" />
<PackageVersion Include="SimpleSecurity.DataProtection" Version="1.0.0" />
<PackageReference Include="SimpleSecurity.DataProtection" />
paket add SimpleSecurity.DataProtection --version 1.0.0
#r "nuget: SimpleSecurity.DataProtection, 1.0.0"
#:package SimpleSecurity.DataProtection@1.0.0
#addin nuget:?package=SimpleSecurity.DataProtection&version=1.0.0
#tool nuget:?package=SimpleSecurity.DataProtection&version=1.0.0
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")andCreateProtector("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 | Versions 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. |
-
net10.0
- Microsoft.AspNetCore.DataProtection.Abstractions (>= 8.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Options (>= 8.0.0)
-
net8.0
- Microsoft.AspNetCore.DataProtection.Abstractions (>= 8.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Options (>= 8.0.0)
-
net9.0
- Microsoft.AspNetCore.DataProtection.Abstractions (>= 8.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Options (>= 8.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.