Skip to content

ProtectedBrowserStorage throws CryptographicException occasionally when using PersistKeysToAzureBlobStorage and ProtectKeysWithAzureKeyVault #48848

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 task done
adamashton opened this issue Jun 16, 2023 · 6 comments
Labels
area-dataprotection Includes: DataProtection

Comments

@adamashton
Copy link

adamashton commented Jun 16, 2023

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Occasionally (25 times in last 30 days) users of my Blazor App are triggering an exception in our Azure hosted App Service.

The key {72bbd744-b488-0000-0000-000000000000} was not found in the key ring. For more information go to http://aka.ms/dataprotectionwarning

System.Security.Cryptography.CryptographicException

Often times the app is working fine and I assume the key is being used to encrypt and decrypt the keys.

Expected Behavior

The App Service should always be able to access the key and the cryptographic exception should not occur.

Steps To Reproduce

I have followed this guide https://learn.microsoft.com/en-us/aspnet/core/security/data-protection/configuration/overview?view=aspnetcore-7.0 and configure my Data Protection like so,

services.AddDataProtection()
  .PersistKeysToAzureBlobStorage(new Uri(options.KeyRingBlobUri), new DefaultAzureCredential())
  .ProtectKeysWithAzureKeyVault(new Uri(options.EncryptionKeyUri), new DefaultAzureCredential());

I believe my Azure access is configured correctly as it works fine for most of the time.

  • App Service has role Storage Blob Data Contributor for the Blob.
  • App Service has role Storage Key Vault Crypto Service Encryption User for the Encryption Key Store.
  • The App is running under a Windows App Service Plan on the .NET 6.0 runtime.
  • Azure.Extensions.AspNetCore.DataProtection.Blobs 1.3.2
  • Azure.Extensions.AspNetCore.DataProtection.Keys 1.2.2

Exceptions (if any)

The key {72bbd744-b488-0000-0000-000000000000} was not found in the key ring. For more information go to http://aka.ms/dataprotectionwarning

System.Security.Cryptography.CryptographicException:
   at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.UnprotectCore (Microsoft.AspNetCore.DataProtection, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
   at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.Unprotect (Microsoft.AspNetCore.DataProtection, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
   at Microsoft.AspNetCore.DataProtection.DataProtectionCommonExtensions.Unprotect (Microsoft.AspNetCore.DataProtection.Abstractions, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
   at Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage.ProtectedBrowserStorage.Unprotect (Microsoft.AspNetCore.Components.Server, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
   at Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage.ProtectedBrowserStorage+<GetAsync>d__8`1.MoveNext (Microsoft.AspNetCore.Components.Server, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Threading.Tasks.ValueTask`1.get_Result (System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)

.NET Version

6

Anything else?

From the code in KeyRingBasedDataProtector it looks like it can access the key but the one with the specific ID is not found?

@ghost ghost added the area-dataprotection Includes: DataProtection label Jun 16, 2023
@ChrisASearles
Copy link

ChrisASearles commented Jun 30, 2023

I also get this error intermittently that I documented on StackOverflow here and cannot figure out what the problem is. Mine does cause a major issue when it occurs. Login happens fine but then I get this error from all API calls from my Blazor WASM app. I believe it has something to do with caching on the client side but cannot figure out why or how to fix it. The only way to get the app functioning again (mine runs in an Azure App Service) is to restart it and make sure the first time I hit the site comes from a private browsing session, then it works fine. If I try from a regular browser window I get the same behavior and nothing works for any users until I restart and hit immediately from a private window.

@mitchdenny mitchdenny added the area-blazor Includes: Blazor, Razor Components label Jul 3, 2023
@surayya-MS surayya-MS removed the area-blazor Includes: Blazor, Razor Components label Jul 4, 2023
@amcasey amcasey added this to the .NET 9 Planning milestone Jan 26, 2024
@ghost
Copy link

ghost commented Jan 26, 2024

Thanks for contacting us.

We're moving this issue to the .NET 9 Planning milestone for future evaluation / consideration. We would like to keep this around to collect more feedback, which can help us with prioritizing this work. We will re-evaluate this issue, during our next planning meeting(s).
If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues.
To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

@dotnet-policy-service dotnet-policy-service bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Feb 6, 2024
@wtgodbe wtgodbe removed the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Feb 6, 2024
@dotnet-policy-service dotnet-policy-service bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Feb 6, 2024
@wtgodbe wtgodbe removed the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Feb 13, 2024
@dotnet dotnet deleted a comment from dotnet-policy-service bot Feb 13, 2024
@dotnet dotnet deleted a comment from dotnet-policy-service bot Feb 13, 2024
@wzoet
Copy link

wzoet commented Jul 11, 2024

We use somewhat the same configuration in our software:

services.AddDataProtection().SetApplicationName(Configuration["DataProtectionApplicationName"]).PersistKeysToDbContext<DataProtectionKeysContext>().ProtectKeysWithAzureKeyVault(new Uri(Configuration["SomeSetting"]), new DefaultAzureCredential());

We use the SetApplicationName to support multiple deployment slots on Azure. Otherwise our keys fail every time we switch slots. Next to that, we store the keys in the database. It is the same database IDSRV uses to store user credentials, so it is reachable by the application.

At this moment we get intermittent errors as displayed above. But we suspect it has something to do with creating new keys.
We found that the key that was reported as missing was actually just inserted into the database. The creation date of the key was just moments before we saw the first errors occur in the logs.

We have no specific settings in the IDSRV options:

services.AddIdentityServer(options => { options.LicenseKey = this.Configuration["IdentityServerLicenseKey"]; options.EmitStaticAudienceClaim = true; }) .AddAspNetIdentity<ApplicationUser>()

We understand that the issue won't be resolved quickly, but it is a major issue for us. All user interaction with our system is stopped and also inter-application communication is stopped as we use IDSRV authentication there as well. Can we circumvent this for now by disabling the automatic key rotation? Or is there another way we could prevent the issue from happening?

We use .Net 8.0 and IDSRV 7.0.1
We also use IDSRV on two app-service-plan instances, we have IDistributedCache set up using the database as well.

Thanks in advance for thinking along.

@nickfortyau
Copy link

This is also a huge issue for our team. Pretty much all database access is halted until the user clears their localStorage.

@Dayana-mypos
Copy link

I have come across this problem as well. A .Net API project is using an instance of Identity Server for authentication. Between midnight and 2.a.m the users successfully create tokens, but moments later introspect returns that the tokens are inactive. There is an error logged between these 2 operations: The key {xxx} was not found in the key ring. For more information go to https://aka.ms/aspnet/dataprotectionwarning

@amcasey
Copy link
Member

amcasey commented Oct 4, 2024

I'm going to close this issue. Please post additional feedback in #36157 so we can track it centrally and don't drop any. My apologies to anyone whose feedback was missed in the interim.

@amcasey amcasey closed this as completed Oct 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-dataprotection Includes: DataProtection
Projects
None yet
Development

No branches or pull requests

9 participants