在.NET Core中加载Azure密钥保险库的秘密是什么更好的?
我们已将ConnectInsTring值存储在Azure密钥库中。我已经阅读了两种方法来从我的.NET Core应用程序中获取秘密价值。 一种是使用以下代码加载configurationManager中的所有秘密:
var keyVaultUrl = builder.Configuration["KeyVaultUrl"];
builder.Host.ConfigureAppConfiguration(builder =>
{
builder.AddAzureKeyVault(new Uri(keyVaultUrl), new DefaultAzureCredential());
});
另一种方法是使用以下代码并获取秘密值:
string keyVaultName = Environment.GetEnvironmentVariable("KEY_VAULT_NAME");
var kvUri = "https://" + keyVaultName + ".vault.azure.net";
var client = new SecretClient(new Uri(kvUri), new DefaultAzureCredential());
var secret = await client.GetSecretAsync(secretName);
在静态类中保存这些值一次,并在需要时通过应用程序使用。
哪种选项在什么情况下更适合。
还提供一些有关重新加载秘密值的输入,如果它更改。
我只有几个秘密
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尽管两种情况都可以重复使用值的方式,但i do 都有个人喜好:使用第一个选项。
这种方法的最大优势是可以通过
iconfiguration
检索值。这意味着在整个应用程序的其余部分中,开发人员甚至不必知道价值的来源。他们可以从ICONFIGURATION
获得设置,无论设置的来源如何。使用您的第二种方法,仍然可以重复使用从密钥库中获得值的代码,但是开发人员需要知道从哪里获得值。就重新加载而言:在
azurekeykeyvaultConfigurationOptions
的情况下,请查看通过。这具有ReloAdinterval
属性,这是...来源:
甚至更丰富的解决方案是将应用程序配置和密钥库组合在一起,以使您重新加载秘密和钥匙库自动的秘密和证书。
Although both scenarios enable reusing the way of getting values, I do have a personal preference: use the first option.
The big plus for this approach is that the values are retrievable through
IConfiguration
. This means that throughout the rest of the application, developers don't even have to know where values come from. They can get settings fromIConfiguration
, no matter the origin of the setting. With your second approach, the code to get a value from Key Vault is still reusable but developers need to know where to get values from.As far as reloading goes: have a look at passing in an instance of
AzureKeyVaultConfigurationOptions
. This has aReloadInterval
property, which is a ...Source: Azure Key Vault configuration provider in ASP.NET Core - Configuration options
An even richer solution would be to combine App Configuration and Key Vault to enable you to Reload secrets and certificates from Key Vault automatically.