【问题标题】:App Settings config section from Azure Key VaultAzure Key Vault 中的应用设置配置部分
【发布时间】:2021-03-04 14:43:20
【问题描述】:

我正在使用新库 Micorosft.Identity.Web 在 Net5 API 上实现 AAD 身份验证。该库公开了一个仅接受 IConfiguration 的方法,其中 app.settings 中的部分如下所示

来自 Startup.cs 上 Micorosft.Identity.Web 的身份验证方法

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddMicrosoftIdentityWebApiAuthentication(Configuration, "AzureAd");
    ...
}

app.settings.json

"AzureAd": {
    "Domain": "contoso.com",
    "Instance": "https://login.microsoftonline.com/",
    "TenantId": "00000000-0000-0000-0000-000000000000",
    "ClientId": "00000000-0000-0000-0000-000000000000"
 },

现在,问题是,我正在使用 Azure 来部署这个 API,当然所有这些敏感值都存储在 Key Vault 中。所以,我想找到一种方法:

services.AddMicrosoftIdentityWebApiAuthentication(new Configuration {
    Domain   = KeyVaultClient.GetSecret("domain"),
    Instance = KeyVaultClient.GetSecret("instance")
    TenantId = KeyVaultClient.GetSecret("tenant")
    ClientId = KeyVaultClient.GetSecret("client")
});

同时,我找不到在 KeyVault 中创建此“部分”的方法,因此我可以这样做

services.AddMicrosoftIdentityWebApiAuthentication(KeyVaultClient.GetSecret("azureadconfig"));

如果我在 Key Vault 上拥有所有值,我该如何归档这些选项之一,或者如何避免取决于 app.settings

编辑注释

我将 Key Vault 作为配置提供程序,但我不知道如何以 Section 的方式返回这些值,正如方法所期望的那样

【问题讨论】:

  • 只是为了确认一下,如果您在 Azure 应用服务中运行,请使用:docs.microsoft.com/en-us/azure/app-service/…
  • 问题是没有从 Key Vault 获取值,问题是 Key Vault 将该值作为 IConfiguration 的“部分”获取
  • 那没有回答我的问题。如果您在应用服务中运行,所有应用设置都会加载到环境变量中以便于使用 - 直接从 ENV 或通过 IConfiguration 等
  • 我确实有一个 azure 应用程序服务,并且我确实将 Key Vault 作为配置提供程序提供了所有这些变量,问题是没有获取这些值,我可以轻松地单独访问所有这些变量,但该方法期望来自配置提供程序的部分,所以我不知道是从这个单独的值形成这个部分。
  • IIRC 您可以在您的秘密名称中使用双破折号,.NET 会将它们视为部分。密码:Section--Itemname

标签: c# .net azure azure-keyvault microsoft-identity-web


【解决方案1】:

您可以在 Key Vault 机密名称中使用双破折号,.NET 会将它们视为部分。密名:Section--Itemname

【讨论】:

    【解决方案2】:

    首先,这些值都不是秘密。所以它们不一定需要在 Key Vault 中。

    但是,如果您仍然想要它们,则需要将 Key Vault 添加为配置提供程序。 然后 Key Vault 机密将通过 IConfiguration 提供,就像 appsettings.json 中的设置一样。

    我写了一篇关于该主题的文章(也使用托管身份连接到 Key Vault):https://joonasw.net/view/aspnet-core-azure-keyvault-msi

    【讨论】:

    • 我有 Key Vault 作为配置提供程序,但该函数需要一个“部分”,密钥库如何返回此部分
    • ASP.NET Core 中的所有设置实际上都是单一设置。如果您有两个环境变量“Section__Setting1”和“Section__Setting2”,它将与 JSON { "Section": { "Setting1":"a","Setting2":"b" } } 一样工作。 JSON 配置提供程序只是将对象解释为具有相同的键前缀。在 Key Vault 中,您将设置添加为 Section--SettingName
    猜你喜欢
    • 2019-12-01
    • 1970-01-01
    • 2016-07-04
    • 2019-09-06
    • 2019-09-20
    • 1970-01-01
    • 2020-08-29
    • 2021-05-20
    • 2021-08-23
    相关资源
    最近更新 更多