Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure web app sporadically throws CryptographicException: Keyset does not exist

I have a Identity Server web app hosted on Azure. It has a .pfx file in it's root directory for signing. The problem is that when newly published it works perfectly fine but after some time it starts throwing CryptographicException: Keyset does not exist.

Based on CryptographicException KeySet does not exists I would assume that it is a file access issue, but why out of the sudden azure is messing up with file access.

like image 281
Tamerlane Avatar asked Sep 15 '16 14:09

Tamerlane


1 Answers

I've been seing the same exception sporadically. In my case, it was caused by Data Protection keys changing when I swap between deployment slots. When using services.AddDataProtection() with the default configuration and hosting the app on Azure App Service, the data protection keys are stored in %HOME%\ASP.NET\DataProtection-keys. This directory is backed by a network share to make sure the keys are available on all your app instances, BUT this is not the case for deployment slots. So when you switch from one deployment slot to another, the keys will change and you will see CryptographicException: Keyset does not exist when your app tries to unprotect a payload.

To make sure your app is always using the same key, you need to configure a different key storage provider. I.e. use Redis or Azure Blob Storage as backing store.

You can find more information about how to configure this in the official documentation.

I've also described the issue I had on my blog.

like image 88
henningst Avatar answered Oct 13 '22 00:10

henningst