首页 > 解决方案 > .net 核心密钥库链接失败,本地环境中的访问被禁止

问题描述

我正在运行以下功能作为我的 .net 核心 Web 应用程序的 Main 方法的一部分

private static void LinkKeyVault(IConfigurationBuilder config, string keyVaultEndpoint)
        {
            if (!string.IsNullOrEmpty(keyVaultEndpoint))
            {
                var azureServiceTokenProvider = new AzureServiceTokenProvider();
                var keyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback));
                config.AddAzureKeyVault(keyVaultEndpoint, keyVaultClient, new DefaultKeyVaultSecretManager());
            }
        }

当我在本地开发机器上运行此代码时,我收到以下错误“操作返回了无效的状态代码‘禁止’”。当此代码在应用服务用户分配的托管标识下在 Azure 中运行时,一切正常。在我的本地环境中,我使用我的 Azure AD 用户登录,该用户使用密钥保管库访问策略获得访问权限,权限与用户分配的托管标识相同。

在此处输入图像描述

在此处输入图像描述

dfrds-dev-web-identity 是用户分配的托管标识,DFRDDevelopers 是我的 Azure AD 帐户所属的组。

标签: azureazure-keyvault

解决方案


It should work, please make sure the group in which the user account located is a Security group, not a Microsoft 365 group, just the Security group is supported in this feature.

Reference - https://docs.microsoft.com/en-us/azure/key-vault/general/secure-your-key-vault#data-plane-and-access-policies

To grant data plane access to several users, create an Azure AD security group and add users to that group.

Also, if you want to use Visual Studio to auth, make sure you logged in with the correct account, and try to use RunAs=Developer; DeveloperTool=VisualStudio in the code to make sure it uses the Visual Studio to auth.

var azureServiceTokenProvider = new AzureServiceTokenProvider(RunAs=Developer; DeveloperTool=VisualStudio);

推荐阅读