NCode.Extensions.DataProtection 1.0.1

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

ci Nuget

NCode.Extensions.DataProtection

A .NET library that provides extensions and utilities for ASP.NET Core Data Protection, including a type-safe factory pattern for creating IDataProtector instances and high-performance span-based protection operations.

Features

  • Type-Safe Data Protector Factory (IDataProtectorFactory<T>)

    • Creates IDataProtector instances with purpose strings derived from generic type parameters
    • Ensures cryptographic isolation between different components automatically
    • Supports custom factory implementations for advanced scenarios
  • High-Performance Span Extensions (DataProtectorExtensions)

    • ProtectSpan<TWriter> - Protects plaintext data using ReadOnlySpan<byte> input and IBufferWriter<byte> output
    • UnprotectSpan<TWriter> - Unprotects data using ReadOnlySpan<byte> input and IBufferWriter<byte> output
    • Reduces memory allocations compared to standard array-based methods
    • Leverages native ISpanDataProtector on .NET 11.0+ when available
    • Includes security measures like memory pinning and secure memory clearing on older frameworks
  • Dependency Injection Integration

    • AddDataProtectorFactory() - Registers the open generic factory for any IDataProtectorFactory<T>
    • AddDataProtectorFactory<T, TImplementation>() - Registers a custom factory implementation for a specific type
    • Uses TryAdd semantics to avoid overwriting existing registrations

Installation

dotnet add package NCode.Extensions.DataProtection

Usage

Basic Setup

// Configure Data Protection (your responsibility to set up the keyring)
services.AddDataProtection()
    .PersistKeysToFileSystem(new DirectoryInfo(@"c:\keys"))
    .ProtectKeysWithDpapi();

// Register the data protector factory
services.AddDataProtectorFactory();

Using the Factory

public class MyService
{
    private readonly IDataProtector _protector;

    public MyService(IDataProtectorFactory<MyService> factory)
    {
        _protector = factory.CreateDataProtector();
    }

    public string Protect(string data) => _protector.Protect(data);
    public string Unprotect(string data) => _protector.Unprotect(data);
}

Using Span Extensions

var protector = factory.CreateDataProtector();
var writer = new ArrayBufferWriter<byte>();

// Protect data
ReadOnlySpan<byte> plaintext = "sensitive data"u8;
protector.ProtectSpan(plaintext, ref writer);

// Unprotect data
var unprotectWriter = new ArrayBufferWriter<byte>();
protector.UnprotectSpan(writer.WrittenSpan, ref unprotectWriter);

Requirements

  • .NET 8.0, .NET 9.0, or .NET 10.0+
  • Microsoft.AspNetCore.DataProtection

Known Limitations

UnprotectSpan Memory Pinning (Pre-.NET 11.0)

⚠️ Security Notice: On frameworks prior to .NET 11.0, UnprotectSpan cannot guarantee that sensitive plaintext data won't be copied by the garbage collector before memory pinning is applied.

When using UnprotectSpan on .NET 8.0, 9.0, or 10.0, the implementation:

  1. Calls IDataProtector.Unprotect() which returns a byte[] containing the plaintext
  2. Immediately attempts to pin the array using GCHandle.Alloc(..., GCHandleType.Pinned)
  3. Copies the data to the destination buffer
  4. Clears the memory using CryptographicOperations.ZeroMemory()

The limitation: Between steps 1 and 2, there is a brief window where the GC could relocate the plaintext array, potentially leaving copies of sensitive data in memory that cannot be cleared.

While the likelihood of this occurring is extremely low in practice, applications with strict security requirements should be aware of this limitation. The ZeroMemory call remains the primary security measure for clearing sensitive data.

On .NET 11.0+, the native ISpanDataProtector interface is used when available, which eliminates this limitation entirely.

License

Licensed under the Apache License, Version 2.0. See LICENSE.txt for details.

Release Notes

  • v1.0.0 - Initial release
  • v1.0.1 - Fix xmldoc
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 79 1/22/2026
1.0.0 71 1/22/2026

Built on 2026-01-22 22:41:54Z