数据保护问题 API (C#)

Prolems with DataProtection API (C#)

我有一个带有多个部署槽的 Azure WebApp。 我希望每个部署槽都有自己的数据保护密钥。 密钥应在部署之间保留。

Uri storageUri = new Uri(blobStorageSasUri);
CloudBlobClient blobClient = new CloudBlobClient(storageUri);
CloudBlobContainer container = blobClient.GetContainerReference("identityserver4dataprotectionkeys");
container.CreateIfNotExistsAsync().GetAwaiter().GetResult();
services.AddDataProtection()
    .SetApplicationName($"{_env.EnvironmentName}_identityserver4")
    .PersistKeysToAzureBlobStorage(container, $"{_env.EnvironmentName}/dataprotectionkeys.xml");

奇怪的是,应用程序搜索 blob 存储中不存在的其他键。例如。这个例外:

CryptographicException: The key {3c2e44fa-dbeb-4547-8d1d-40f5eed15590} was not found in the key ring.

在 blob 存储中,我有密钥 ID:6bed1559-9bf7-42af-83fd-85fb417c4edc。

此外,blob 存储始终只有一个键。

我在实现这个 API 时遇到了真正的麻烦。非常感谢您的帮助!

您能否确认 3c2e44fa-dbeb-4547-8d1d-40f5eed15590 存在于属于另一个环境的博客存储中?你能检查一下 EnvironmentName returns 是否是预期值吗?

我使用密钥保管库解决了这个问题。

public void ConfigureServices(IServiceCollection services)
{
services.AddDataProtection()
    .PersistKeysToAzureBlobStorage(new Uri("<blobUriWithSasToken>"))
    .ProtectKeysWithAzureKeyVault("<keyIdentifier>", "<clientId>",         "<clientSecret>");
}

来源:https://docs.microsoft.com/en-us/aspnet/core/security/data-protection/configuration/overview?view=aspnetcore-2.1&tabs=aspnetcore2x#protectkeyswithazurekeyvault