如何从 Azure Key Vault 读取整个部分?

问题描述 投票:0回答:1

我有一个 Azure Key Vault,我在其中使用以下方式保存我的应用程序机密:

秘密名字价值部分--秘密1价值1部分--秘密2价值2部分--秘密3价值3
所以如果我将其表示为 JSON,它将如下所示

{ "Section": { "Secret1":"Value1", "Secret2":"Value2", "Secret3":"Value3", } }
这里我面临的问题是,当我使用配置从 Key Vault 读取机密时,我没有获取值。但当我读到个人价值时,我明白了。

喜欢,

当我阅读

Configuration["Section:Secret1"]

时,我得到了价值。
同时,如果我读到
Configuration["Section"]
,我就会变得空虚。

有什么方法可以读取 Key Vault 中的整个部分吗?

我已经提到了

这个。但它没有提供有关如何阅读整个部分的详细信息。如果按键数量增加,则很难读取单个按键。

c# .net-core azure-keyvault
1个回答
0
投票
我创建了一个密钥库并添加了如下秘密:

enter image description here

要从 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}"); }

enter image description here

代码通过

DefaultAzureCredential

 登录 az 进行身份验证,如下所示:

az login

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.