我有一个 Azure Key Vault,我在其中使用以下方式保存我的应用程序机密:
{
"Section": {
"Secret1":"Value1",
"Secret2":"Value2",
"Secret3":"Value3",
}
}
这里我面临的问题是,当我使用配置从 Key Vault 读取机密时,我没有获取值。但当我读到个人价值时,我明白了。喜欢,
当我阅读
Configuration["Section:Secret1"]
时,我得到了价值。 同时,如果我读到
Configuration["Section"]
,我就会变得空虚。有什么方法可以读取 Key Vault 中的整个部分吗?
我已经提到了
要从 Azure Key Vault 检索整个部分,请使用以下代码:
using Azure.Extensions.AspNetCore.Configuration.Secrets;
using Azure.Identity;
using Azure.Security.KeyVault.Secrets;
using Microsoft.Extensions.Configuration;
using System;
var kvUri = new Uri("https://rukkv3333.vault.azure.net/");
var client = new SecretClient(kvUri, new DefaultAzureCredential());
var builder = new ConfigurationBuilder();
builder.AddAzureKeyVault(client, new KeyVaultSecretManager());
var config = builder.Build();
var section = config.GetSection("Section").GetChildren();
foreach (var item in section)
{
Console.WriteLine($"{item.Key}: {item.Value}");
}
代码通过
DefaultAzureCredential
az login