首页 > 解决方案 > 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 时遇到了真正的麻烦。非常感谢帮助!

标签: asp.netidentityserver4data-protection

解决方案


我通过使用 keyvault 解决了这个问题。

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


推荐阅读