Try Playground
Show / Hide Table of Contents

Configuring ASP.NET Core Data Protection Provider

ASP.NET Core provides Data Protection, which allows you to protect your data using different encryption algorithms. The data protection system employs a discovery mechanism by default to determine where the cryptographic keys should persist. The developer can override the default discovery mechanism and manually specify the location.

Note

This feature is available in NCache Enterprise only.

NCache, here, works as a key storage provider to store the keys for data protection services. To configure NCache ASP.NET Core Data Protection Provider, follow the steps below.

Note

This feature is available in NCache 5.3.1 onwards.

Prerequisites for Configuring ASP.NET Core Data Protection Provider

  • .NET
  • Install the following NuGet packages in your application based on your NCache edition:
    • Enterprise: AspNetCore.DataProtection.NCache
  • To utilize the extension, include the following namespaces in your application:
    • Alachisoft.NCache.AspNetCore.DataProtection
  • The cache must be running.
  • For API details, refer to: PersistKeysToNCache.
  • To ensure the operation is fail-safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
  • To handle any unseen exceptions, refer to the Troubleshooting section.

Step 1: Configure Data Protection Service

ASP.NET Core provides its middleware for Data Protection. This has to be added to the service collection using the AddDataProtection method.

  • Open the Startup.cs file of your project.

  • In the ConfigureServices method, add the following service:

public void ConfigureServices(IServiceCollection services)
{
    services.AddDataProtection();
}

Step 2: Install the NuGet Package

Install the NuGet package that allows to persist ASP.NET Core Data Protection keys in NCache. To install the NuGet package, open Visual Studio and go to Tools -> NuGet Package Manager -> Package Manager Console.

Step 3: Configure NCache as a Key Storage Provider for the Data Protection Service

Once the NuGet package has been installed, configure NCache as a key storage provider to store keys for data protection services.

public void ConfigureServices(IServiceCollection services)
{
    string cacheName = "demoCache";
    string cacheTag = "encryptions_keys_tag";
    services.AddDataProtection().PersistKeysToNCache(cacheName, cacheTag);
}

To use logging with the NCache storage provider, enable logging in IServiceCollection.

public void ConfigureServices(IServiceCollection services)
{
    string cacheName = "demoCache";
    string cacheTag = "encryptions_keys_tag";

    services.AddLogging(builder =>
        builder.AddConsole()
    );

    services.AddDataProtection().PersistKeysToNCache(cacheName, cacheTag);
}

Step 4: Use NCache as a Key Storage Provider in ASP.NET Core

To use NCache as a key storage provider in ASP.NET Core Data Protection Provider, implement the sample application below:

string cacheName = "demoCache";
string cacheTag = "encryptions_keys_tag";
var serviceCollection = new ServiceCollection();

serviceCollection.AddLogging
    (builder =>
        builder.AddConsole()
    );

serviceCollection.AddDataProtection()
    .PersistKeysToNCache(cacheName,cacheTag);

var services = serviceCollection.BuildServiceProvider();

// Create an instance of CustomDataProtector using the service provider
var instance = ActivatorUtilities.CreateInstance<CustomDataProtector>(services);
instance.ProtectUnprotectData();

This CustomDataProtector class implements a method that is used for protecting and unprotecting the data.

public class CustomDataProtector
{
    IDataProtector _protector;

    // The 'provider' parameter is provided by DI
    public CustomDataProtector(IDataProtectionProvider provider)
    {
        _protector = provider.CreateProtector("Contoso.MyClass.v1");
    }

    public void ProtectUnprotectData()
    {
        Console.Write("Enter input: ");
        string input = Console.ReadLine();

        // Protect the payload
        string protectedPayload = _protector.Protect(input);
        Console.WriteLine($"Protect returned: {protectedPayload}");

        // Unprotect the payload
        string unprotectedPayload = _protector.Unprotect(protectedPayload);
        Console.WriteLine($"Unprotect returned: {unprotectedPayload}");
    }
}

See Also

.NET: Alachisoft.NCache.AspNetCore.DataProtection namespace.

In This Article
  • Prerequisites for Configuring ASP.NET Core Data Protection Provider
  • Step 1: Configure Data Protection Service
  • Step 2: Install the NuGet Package
  • Step 3: Configure NCache as a Key Storage Provider for the Data Protection Service
  • Step 4: Use NCache as a Key Storage Provider in ASP.NET Core
  • See Also

Contact Us

PHONE

+1 (214) 764-6933   (US)

+44 20 7993 8327   (UK)

 
EMAIL

sales@alachisoft.com

support@alachisoft.com

NCache
  • NCache Enterprise
  • NCache Professional
  • Edition Comparison
  • NCache Architecture
  • Benchmarks
Download
Pricing
Try Playground

Deployments
  • Cloud (SaaS & Software)
  • On-Premises
  • Kubernetes
  • Docker
Technical Use Cases
  • ASP.NET Sessions
  • ASP.NET Core Sessions
  • Pub/Sub Messaging
  • Real-Time ASP.NET SignalR
  • Internet of Things (IoT)
  • NoSQL Database
  • Stream Processing
  • Microservices
Resources
  • Magazine Articles
  • Third-Party Articles
  • Articles
  • Videos
  • Whitepapers
  • Shows
  • Talks
  • Blogs
  • Docs
Customer Case Studies
  • Testimonials
  • Customers
Support
  • Schedule a Demo
  • Forum (Google Groups)
  • Tips
Company
  • Leadership
  • Partners
  • News
  • Events
  • Careers
Contact Us

  • EnglishChinese (Simplified)FrenchGermanItalianJapaneseKoreanPortugueseSpanish

  • Contact Us
  •  
  • Sitemap
  •  
  • Terms of Use
  •  
  • Privacy Policy
© Copyright Alachisoft 2002 - 2025. All rights reserved. NCache is a registered trademark of Diyatech Corp.
Back to top