Skip to content

Commit 6a7ebb0

Browse files
committed
react to feedback
1 parent e058df8 commit 6a7ebb0

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed

aspnetcore/security/data-protection/configuration/scaling.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ The following scenarios do ***NOT*** provide automatic key storage in a shared l
3232

3333
## Managing Data Protection keys outside the app
3434

35-
An app with multiple instances may occasionally see an error like `System.Security.Cryptography.CryptographicException: The key {A6EF5BC2-FDCC-4C0C-A3A5-CDA9A1733D70} was not found in the key ring.`. This can happen when instances get out of sync and data protected on one instance (e.g. an anti-forgery token) is unprotected on another instance (e.g. because a form was served from the former and posted to the latter) that doesn't yet know about that key. When this happens, an app user may have to resubmit a form or re-authenticate (if it was an authentication token that couldn't be unprotected).
35+
An app with multiple instances might encounter a [System.Security.Cryptography.CryptographicException](/dotnet/api/system.security.cryptography.cryptographicexception) with the message `The key {A6EF5BC2-FDCC-4C0C-A3A5-CDA9A1733D70}` `was not found in the key ring.` This error occurs when instances become out of sync, causing data protected on one instance, such as an anti-forgery token, to fail when unprotected on another instance. This can happen, for example, if a form is served by one instance but posted to another that has not yet updated its key ring. When this issue arises, users may need to resubmit a form or re-authenticate if the issue involves an authentication token.
3636

3737
One common reason app instances end up with different sets of keys is that, in the absence of a usable key (e.g. due to expiration, lack of access to the backing repository, etc), an instance will generate a new key of its own. Until that key has propagated to all other instances (which can take up to two days), there's a risk that data protected with that new key will sent to an instance that doesn't know how to unprotect it.
3838

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Azure.Extensions.AspNetCore.DataProtection.Keys" Version="1.3.0" />
11+
<PackageReference Include="Azure.Identity" Version="1.13.1" />
12+
<PackageReference Include="Microsoft.Extensions.Azure" Version="1.9.0" />
13+
<PackageReference Include="Microsoft.AspNetCore.DataProtection" Version="9.0.0" />
14+
</ItemGroup>
15+
16+
17+
</Project>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Azure.Identity;
2+
using Microsoft.AspNetCore.DataProtection;
3+
4+
var hostBuilder = new HostApplicationBuilder();
5+
6+
hostBuilder.Configuration.AddJsonFile("appsettings.json", optional: false, reloadOnChange: false);
7+
8+
var blobStorageUri = hostBuilder.Configuration["AzureURIs:BlobStorage"]!;
9+
var keyVaultURI = hostBuilder.Configuration["AzureURIs:KeyVault"]!;
10+
11+
// Use the same persistence and protection mechanisms as your app
12+
hostBuilder.Services
13+
.AddDataProtection()
14+
.PersistKeysToAzureBlobStorage(new Uri(blobStorageUri), new DefaultAzureCredential())
15+
.ProtectKeysWithAzureKeyVault(new Uri(keyVaultURI), new DefaultAzureCredential());
16+
17+
using var host = hostBuilder.Build();
18+
19+
// Perform a dummy operation to force key creation or rotation, if needed
20+
var dataProtector = host.Services.GetDataProtector("Default");
21+
dataProtector.Protect([]);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
},
8+
"AllowedHosts": "*"
9+
}

0 commit comments

Comments
 (0)