Introduction
In modern web applications, data security is critical, especially when dealing with sensitive information such as authentication cookies, tokens, or personal user data. ASP.NET Core provides a Data Protection API that allows developers to encrypt and protect data in a consistent and secure way.
By default, ASP.NET Core stores cryptographic keys in the file system. However, in production scenarios, especially for load-balanced environments or multiple server instances, it is recommended to store keys in a centralized and secure location. One such option is SQL Server, which allows:
Sharing cryptographic keys across multiple servers
Ensuring consistency and recoverability
Centralized management
In this guide, we will show step-by-step how to configure ASP.NET Core Data Protection with SQL Server for a production-ready setup.
1. Understanding ASP.NET Core Data Protection
ASP.NET Core Data Protection provides:
Key management – Automatic creation, rotation, and storage of keys.
Encryption and decryption – Protect sensitive data such as cookies, tokens, or view state.
Isolation – Keys are unique per application unless shared intentionally.
Common use cases
Protecting authentication cookies
Encrypting tokens for password reset links
Storing sensitive session data
2. Installing Required Packages
To use SQL Server as a key store, install:
dotnet add package Microsoft.AspNetCore.DataProtection.EntityFrameworkCore
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
DataProtection.EntityFrameworkCoreprovides the SQL Server backing store.EntityFrameworkCore.SqlServeris needed to connect to the database.
3. Setting Up SQL Server Table
Create a table for storing data protection keys. You can do this using EF Core migrations.
3.1 Define the Key Entity
using Microsoft.AspNetCore.DataProtection.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
public class DataProtectionKeyContext : DbContext, IDataProtectionKeyContext
{
public DataProtectionKeyContext(DbContextOptions<DataProtectionKeyContext> options)
: base(options) { }
public DbSet<DataProtectionKey> DataProtectionKeys { get; set; }
}
DataProtectionKeyis provided byMicrosoft.AspNetCore.DataProtection.EntityFrameworkCore.IDataProtectionKeyContextis required for EF Core key storage.
3.2 Add Migration and Update Database
dotnet ef migrations add AddDataProtectionKeys
dotnet ef database update
This creates the table DataProtectionKeys in SQL Server.
4. Configuring Data Protection to Use SQL Server
In Program.cs or Startup.cs:
builder.Services.AddDbContext<DataProtectionKeyContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
builder.Services.AddDataProtection()
.PersistKeysToDbContext<DataProtectionKeyContext>()
.SetApplicationName("MyApp") // Important for multi-server scenarios
.ProtectKeysWithCertificate("thumbprint-of-your-certificate"); // Optional: encrypt keysKey Points
PersistKeysToDbContext– Stores keys in SQL Server instead of the file system.SetApplicationName– Ensures all instances of the app share the same key ring.ProtectKeysWithCertificate– Optional, adds an extra encryption layer using an X.509 certificate.
5. Optional: Protect Keys with Certificate
For extra security, you can protect keys using a certificate:
builder.Services.AddDataProtection()
.PersistKeysToDbContext<DataProtectionKeyContext>()
.ProtectKeysWithCertificate(
new X509Certificate2("path-to-certificate.pfx", "certificate-password"));
This ensures that even if the database is compromised, the keys are encrypted.
6. Using Data Protection in ASP.NET Core
6.1 Encrypting and Decrypting Data
Inject IDataProtector into your service or controller:

Join the conversation! Your thoughts help the community grow.