ASP.NET Key Storage on Shared Hosting


You have your ASP.NET 5+ application you have built with Visual Studio running locally but when you publish your application to shared web hosting it stops working with errors that include;

Using an in-memory repository. Keys will not be persisted to storage.

Neither user profile nor HKLM registry available. Using an ephemeral key repository. Protected data will be unavailable when application exits.

ASP.NET 5+ applications do not have access to the key storage, which breaks features including anti-request forgery tokens.

By default .NET key storage will use the User Profile and the Windows Registry on the server for the key storage. In a multi-tenancy environment such as shared hosting write access to the Registry is not available for security reasons. This problem can be fixed by moving key storage to the website's private 'data' folder located above the website's root.

Program.cs (.NET 6 and later)

Most applications built with .NET 6 or later use the minimal hosting model and do not have a Startup.cs file. Add the following to Program.cs, before the line var app = builder.Build(); and replace "example\example.com" with your hosting package username and the domain of your website.

using Microsoft.AspNetCore.DataProtection;

// ... after var builder = WebApplication.CreateBuilder(args);

builder.Services.AddDataProtection()
    .PersistKeysToFileSystem(new DirectoryInfo(@"D:\HostingSpaces\example\example.com\data\keys\"));


Startup.cs (older applications)

If your application has a Startup.cs file, place the example below into the ConfigureServices method, again replacing "example\example.com" with your hosting package username and the domain of your website.

using Microsoft.AspNetCore.DataProtection;

public void ConfigureServices(IServiceCollection services)
{
    services.AddDataProtection()
    .PersistKeysToFileSystem(new DirectoryInfo(@"D:\HostingSpaces\example\example.com\data\keys\"));
}



Back To Hosting Knowledgebase Menu

Windows Dedicated and Hyper-V Servers

Fully managed or self-managed, from our own solar powered data centre.

Web Wiz® is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.

Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, €, prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz® Ltd. All rights reserved.