Skip to main content

CryptographicException: An error occurred while trying to encrypt the provided data. Refer to the inner exception for more information

I created a new Blazor Server app in Visual Studio 2019 and tried to run it. But I was getting this error

CryptographicException: An error occurred while trying to encrypt the provided data. Refer to the inner exception for more information.

I couldn't find any reason or solution to this problem. I tried creating the project multiple times but same error.

I created a new .Net Core Web App and added a new razor component and included that component in a razor page (cshtml file) like this

@(await Html.RenderComponentAsync<GeofenceWork>(RenderMode.ServerPrerendered))

and

<component type="typeof(GeofenceWork)" render-mode="serverprerendered" />

As soon as I navigate to this page that has component added I got the same error:

CryptographicException: An error occurred while trying to encrypt the provided data. Refer to the inner exception for more information.

This was very frustrating. After hours of trying and searching I figured out the solution. 

Solution

There is a folder named DataProtection-Keys in %LOCALAPPDATA%/ASP.NET and inside that folder there are multiple xml files starting with "key-". One of them was corrupt and had NULL data. I deleted that file and everything started to run fine from Visual Studio. Both Blazor Server App and .NET Core Web App with razor component added.

%LOCALAPPDATA% in Windows is C:\Users\{yourusername}\AppData\Local

So the full path to DataProtection-Keys will be

C:\Users\{yourusername}\AppData\Local\ASP.NET\Local

Comments