azure - 使用客户端 key 检索 Azure KeyVault key

标签 azure azure-keyvault azure-managed-identity

我正在尝试各种 Azure 功能,目前想要从 KeyVault 检索 secret 。

直接进入案例:

我正在使用this nuget 包与我的 azure 资源交互。

我开发了一个简单的 .NET Core 控制台应用程序并在本地运行它。

我有一个 KeyVault 资源,其中定义了一个 key ,该 key 处于事件状态且未过期。

我已在 AAD 中注册了一个应用程序,因此我本地发布的 .NET Core 控制台应用程序在 AAD 中具有身份。

我已经在 AAD 中的这个注册应用程序中创建了一个“客户端 key ”,以使用它来验证自己作为应用程序的身份。

之后,我在 KeyVault 资源中添加了访问策略,以允许对该注册应用程序的 secret 进行 GET 操作: KeyVault access policies

然后我开发了一小段代码,它应该检索所需的 secret :

public class AzureAuthentication
{
    public async Task<string> GetAdminPasswordFromKeyVault()
    {
        const string clientId = "--my-client-id--";
        const string tenantId = "--my-tenant-id--";
        const string clientSecret = "--my-client-secret--";
        var credentials = new ClientSecretCredential(tenantId, clientId, clientSecret);
        var client = new SecretClient(new Uri("https://mykeyvaultresource.vault.azure.net"), credentials);
        var secret = await client.GetSecretAsync("admincreds");
        return secret.Value.Value;
    }
}

但是,当我尝试执行此操作时,我收到 AccessDenied 错误: error

我在这里错过了一些非常明显的事情吗?或者应用 KeyVault 资源中的访问策略屏幕中的更改时存在一些延迟(目前>30 分钟)?

最佳答案

我测试了您的代码并获取权限,它工作正常。

enter image description here

从您的屏幕截图来看,您似乎没有将与 AD 应用相关的正确服务主体添加到访问策略中。

如果您添加与 AD 应用相关的服务主体,它将显示为 APPLICATION,而不是 COMPOUND IDENTITY

enter image description here

enter image description here

所以添加时,您可以直接搜索客户端Id(即应用程序Id)您的应用注册名称,确保添加正确一个。

enter image description here

关于azure - 使用客户端 key 检索 Azure KeyVault key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62004712/

相关文章:

Azure 存储帐户拒绝托管服务标识的颁发者

azure - 使用托管标识和基于 token 的登录从 Azure Function 访问 Azure Analysis Services

azure - Azure Functions PowerShell API 的 OpenAPI 规范配置

json - Azure - 应用程序注册 - 编辑 OAuth2Permissions 的现有条目不起作用

azure - Bicep 模板中的引用对象变量

Azure Function App 通过应用程序设置使用最新版本的 Key Vault Secret

azure - 使用 databricks 集群管理身份

Azure 托管标识错误 - 找不到访问 token 的标识

powershell - 通过 PowerShell 将 ASP.NET Core 应用程序发布到 Azure 在 Azure 中收到 502(错误网关)

国家云的 Azure Key Vault 域名