Winform 应用程序 - 从 KeyVault 获取 Azure 机密时出错“DefaultAzureCredential 无法从包含的凭据中检索令牌。”

问题描述 投票:0回答:1

我有用 C# 编写的 winform 应用程序。我正在尝试从 Azure KeyVault 获取蔚蓝机密。我写了下面的函数来获取秘密。

    private async Task<string> GetSecretFromAzureKV(string secretName)
    {
        try
        {
            var keyVaultName = "my-Keyvault-name";
            var kvUri = $"https://{keyVaultName}.vault.azure.net";

            var credentials = new DefaultAzureCredential();
            var client = new SecretClient(new Uri(kvUri), credentials);

            var secret = await client.GetSecretAsync(secretName);

            return secret.Value.Value;
        }
        catch (Exception ex)
        {

            throw;
        }
    }

当我调试此解决方案时,我可以看到以下错误。

另请注意,我没有在 Azure VM 上运行它。该代码位于我的本地计算机上。 任何帮助是极大的赞赏。谢谢。

c# azure winforms azure-keyvault
1个回答
0
投票

在 Azure Key Vault 中,检查您是否有以下配置

Access configuration

如果您有

Vault access policy
,您将能够添加秘密并检索它们,没有任何问题。

如果您有

Azure role-based access control
,那么您必须拥有分配给服务主体或用户的 Key Vault 相关角色(
KeyVault Administrator
KeyVault Secret User
)。

enter image description here

  • 并在
    Access policies
    中,确保您拥有包含您的 ID/姓名的访问策略,并拥有
    Get/List
    密钥/秘密权限。

enter image description here

  • 另请检查您的 Key Vault Secret 是否已启用。

enter image description here

从 KeyVault 获取 Azure 机密时出错“DefaultAzureCredential 无法从包含的凭据中检索令牌。”

这些是默认消息,将显示在

DefaultAzurecredential
中。

enter image description here

  • 即使收到警告,我也能够成功检索秘密。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.