---
title: "Persist Data Protection Keys to Database"
description: "ASP.NET Zero is the base solution for building robust and scalable web applications. Unlock your full potential today!"
url: "https://aspnetzero.com/blog/persist-data-protection-keys-to-database"
image: "https://aspnetzero.com/Images/Blog/persist-data-protection-keys-to-database.png"
---

[Blog](https://aspnetzero.com/blog)

# Persist Data Protection Keys to Database

24 October 2022 2 min read [Security](https://aspnetzero.com/blog/topics/security)

![Persist Data Protection Keys to Database](https://aspnetzero.com/Images/Blog/persist-data-protection-keys-to-database.png)

Share [X](https://twitter.com/intent/tweet?url=https%3A%2F%2Faspnetzero.com%2Fblog%2Fpersist-data-protection-keys-to-database&text=Persist+Data+Protection+Keys+to+Database) [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Faspnetzero.com%2Fblog%2Fpersist-data-protection-keys-to-database) [Reddit](https://reddit.com/submit?url=https%3A%2F%2Faspnetzero.com%2Fblog%2Fpersist-data-protection-keys-to-database&title=Persist+Data+Protection+Keys+to+Database) [Hacker News](https://news.ycombinator.com/submitlink?u=https%3A%2F%2Faspnetzero.com%2Fblog%2Fpersist-data-protection-keys-to-database&t=Persist+Data+Protection+Keys+to+Database)

# Persist Data Protection Keys to Database in ASP.NET Zero

ASP.NET Core provides a very sophisticated approach for protecting data. You can check [ASP.NET Core Data Protection Documentation](https://learn.microsoft.com/en-us/aspnet/core/security/data-protection/introduction) for more information.

Just like you do in any other ASP.NET Core app, if you want to use **data protection** in ASP.NET Zero, you can easily configure it. However, if you want to store data protection keys in **database** (see [documentation](https://learn.microsoft.com/en-us/aspnet/core/security/data-protection/configuration/overview)), you will face an error. That's because ASP.NET Core will try to create configured DbContext but at that time DbContext will not be configured by ASP.NET Zero. To overcome this problem, we need to configure data protection **a bit different**.

## Default Configuration

First of all, you need to add related NuGet package to your project and add `DataProtectionKey` entity to your DbContext as explained [here](https://learn.microsoft.com/en-us/aspnet/core/security/data-protection/configuration/overview). We will just configure data projecttion in a different way.

## Dependency Injection

By default, ASP.NET Core's data protection must be configured in `Startup.cs` file. However, in our case, we must configure it in ASP.NET Zero's EF Core module. To do that, we must access the `IServiceCollection` in our EF Core module.

Create classes below in the project;

```csharp
public interface IServiceCollectionProvider 
{
    IServiceCollection ServiceCollection { get; }
}

public sealed class ServiceCollectionProvider: IServiceCollectionProvider
{
    public ServiceCollectionProvider(IServiceCollection serviceCollection)
    {
        ServiceCollection = serviceCollection;
    }

    public IServiceCollection ServiceCollection { get; }
}
```

Then, we can register IServiceCollectionProvider in our Startup.cs file as shown below;

```csharp
services.AddSingleton<IServiceCollectionProvider>(new ServiceCollectionProvider(services));
```

## Configure Data Protection

Finally, configure data protection in the `PostInitialize` method of the EFCore module as shown below;

```csharp
var serviceCollectionProvider = IocManager.IocContainer.Resolve<IServiceCollectionProvider>();
serviceCollectionProvider.ServiceCollection.AddDataProtection().PersistKeysToDbContext<YourDbContext>()
	.SetApplicationName("YourApplicationName");
```

## Ready to build?

Get full source code, premium support, and enterprise-ready features with ASP.NET Zero.

[See pricing plans](https://aspnetzero.com/pricing)

## Have questions before purchasing?

Schedule a free consultation with our team to find the right plan for your project.

[Schedule a meeting](https://aspnetzero.com/schedule-meeting)

## You might also like

### [Business Benefits of Using ASP.NET Zero for Enterprise Software Development](https://aspnetzero.com/blog/business-benefits-of-aspnetzero)

11 September 2026

### [Common Mistakes and Errors When Hosting ASP.NET Core Apps](https://aspnetzero.com/blog/common-mistakes-and-errors-hosting-aspnet-core-apps)

26 August 2026

### [Using AutoMapper After Migrating to Mapperly](https://aspnetzero.com/blog/using-automapper-after-migrating-to-mapperly)

04 August 2026

## Recent posts

### [Business Benefits of Using ASP.NET Zero for Enterprise Software Development](https://aspnetzero.com/blog/business-benefits-of-aspnetzero)

11 September 2026

### [Common Mistakes and Errors When Hosting ASP.NET Core Apps](https://aspnetzero.com/blog/common-mistakes-and-errors-hosting-aspnet-core-apps)

26 August 2026

### [Using AutoMapper After Migrating to Mapperly](https://aspnetzero.com/blog/using-automapper-after-migrating-to-mapperly)

04 August 2026

## Start building with ASP.NET Zero

Full source code, premium support, Angular, React & MVC UI options.

[View pricing](https://aspnetzero.com/pricing)

## Topics
