【问题标题】:net core app fails to access azure key vaultnet core 应用程序无法访问 azure key Vault
【发布时间】:2018-05-22 16:14:00
【问题描述】:

我正在尝试使用 Azure 密钥保管库来存储一些机密。按照本教程: https://docs.microsoft.com/en-us/azure/key-vault/vs-secure-secret-appsettings

在调试时工作,但是当我将应用程序发布到 Azure 时,它​​会因为我的密钥返回 null 而失败。

然后我尝试了这个教程:https://medium.com/@patoncrispy/securing-settings-in-dot-net-core-apps-using-azure-key-vault-d4ec82de00b8

这在调试期间也有效,但是当我发布到 Azure 时,它​​也会失败,因为我的密钥返回 null。

任何关于我应该如何解决这个问题的帮助都会很棒。为什么我会在生产中得到 null,但在提供相同的凭据时却无法调试?

【问题讨论】:

  • 您是否在 Azure 中启用了托管服务标识?
  • @juunas 我没有,但我现在有了。是否必须在launchsettings.json 或azure 中的appsettings 中指定环境变量?
  • Appsettings 成为环境变量。但是你在 env vars 中添加了什么?
  • 第一个链接使用 en vars 作为保管库 url。我对其进行了硬编码,现在它失败了,Azure 的流日志中没有堆栈跟踪输出。所以现在我有一个完全不同的错误。保险库也不显示请求
  • 好的,由于某种原因,流日志需要 2 分钟才能显示堆栈跟踪及其与我的 DI 服务相关的信息。所以是的......所要做的就是翻转应用程序的身份哈哈。太感谢了!添加作为信用的答案。

标签: azure asp.net-core azure-keyvault


【解决方案1】:

为了让应用能够使用 AppAuthentication 库访问 Azure 中的 Key Vault,它应该启用托管服务标识。

此外,自动创建的服务主体需要被授予对 Key Vault 中机密的访问权限。

在 Azure 中指定 KeyVault 端点 URL 可以在第一个示例中使用带有密钥 KEYVAULT_ENDPOINT 的应用设置来完成。

【讨论】:

    【解决方案2】:
    // Create a new secret client using the default credential from Azure.Identity using environment variables previously set,
    // including AZURE_CLIENT_ID, AZURE_CLIENT_SECRET, and AZURE_TENANT_ID.
    var client = new SecretClient(vaultUri: new Uri(keyVaultUrl), credential: new DefaultAzureCredential());
    
    // Create a new secret using the secret client.
    KeyVaultSecret secret = client.SetSecret("secret-name", "secret-value");
    
    // Retrieve a secret using the secret client.
    secret = client.GetSecret("secret-name");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-09
      • 1970-01-01
      相关资源
      最近更新 更多