如何正确存储和提取AZURE_TENANT_ID、AZURE_CLIENT_ID和AZURE_CLIENT_SECRET?

How to properly store and extract AZURE_TENANT_ID, AZURE_CLIENT_ID and AZURE_CLIENT_SECRET?

我有一个 Azure 密钥保管库,其中存储了一个 api 密钥。 我已通过 Visual studio 将 Azure Key Vault 作为连接服务添加到项目中。

我尝试通过此代码将我的键值存储在 akv 中

    HttpClient client = new HttpClient();
    string keyVaultName = !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("web-kv")) ? Environment.GetEnvironmentVariable("web-kv") : throw new ArgumentNullException("web-kv");
    string kvUri = "https://" + keyVaultName + ".vault.azure.net";
    bool success = Uri.TryCreate(kvUri, UriKind.Absolute, out Uri uri);
    if(!success)
    {
        throw new Exception(kvUri);
    }
        var akvClient = new SecretClient(new Uri(kvUri), new DefaultAzureCredential());
        var apiKey = akvClient.GetSecret("LicenseApiKey");

哪个returns这个异常

Azure.Identity.AuthenticationFailedException: The DefaultAzureCredential failed to retrieve a token from the included credentials.
  EnvironmentCredential is unavailable Environment variables not fully configured. AZURE_TENANT_ID and AZURE_CLIENT_ID must be set, along with either AZURE_CLIENT_SECRET or AZURE_USERNAME and AZURE_PASSWORD. Currently set variables [  ].
  ManagedIdentityCredential is unavailable No managed identity endpoint found..
  SharedTokenCacheCredential is unavailable No accounts were discovered in the shared token cache. To fix, authenticate through tooling supporting azure developer sign on..

应用未在azure中注册为APP,不知为何检测不到环境变量已设置?

在开发环境中,您需要按照描述使用 SecretManager here

Here 你可以找到一个工作样本的深度挖掘。

我最终使用了一种方法,在部署时让 Azure 管道将机密插入我的 appSetting,因此代码不会与 keyvault 交互。