1

我有这个天蓝色的密钥库,其中存储了一个 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");

返回此异常

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,并且由于某种原因它没有检测到设置了环境变量?

4

2 回答 2

0

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

于 2022-02-18T08:46:32.210 回答
-1

在开发环境中,您需要按照此处所述使用 SecretManager 。

在这里,您可以找到带有工作示例的深入研究。

于 2022-02-12T11:39:18.620 回答