PersistKeysToDb 1.0.1

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

Introduction

Data Protection Api uses symmetric cryptographic algorithms to protect data. So it needs to store and access the key somewhere safe. It handles all the key rotation and has a very simple API. When using data protection api in a dotnet project you have the option to store the keys in different locations. While there is a EF package to store the keys in a database, there was no Dapper provider for this purpose. That's why I made this package.

How to use

You need a database table that has three columns:

  • Id of type int
  • FriendlyName of type varchar(max)
  • Xml of type varchar(max)

If you specify an explicit key persistence location, the data protection system deregisters the default key encryption at rest mechanism, so keys are no longer encrypted at rest. It's recommended that you additionally specify an explicit key encryption mechanism for production deployments.

Set up the database

The package does not create the database tables. But they are very simple ones. Lets say you have a SQL database called DataProtectionKyeDb, then you can use this script to create the table.

USE [DataProtectionKeyDb]
GO

/****** Object:  Table [dbo].[DataProtectionKey]    Script Date: 20/12/2024 10:21:09 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[DataProtectionKey](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[FriendlyName] [varchar](max) NULL,
	[Xml] [varchar](max) NOT NULL,
PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO


Register the package

internal class Program
    {
        private static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            builder.Services.AddDataProtection()
            .ProtectKeysWithCertificate("Certificate") // recommended to protect keys at rest 
            .PersistKeysToDb("Connection string"); // extension method from this package

            . . .

        }
    }

Other options

Key storage providers discusses multiple package from Microsoft.

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 was computed.  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 was computed.  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 858 11/28/2025

Initial public release on NuGet.org