azure - 从 .Net 应用程序访问 Azure Key Vault - 获取 DefaultAzureCredential 身份验证失败

标签 azure asp.net-core azure-keyvault

我正在尝试将我的 .Net Core 3.1 应用连接到 Azure Key Vault。我已经关注了quickstart教程,并收到以下错误:

Microsoft.Extensions.Configuration.AzureAppConfiguration.KeyVaultReferenceException: 'DefaultAzureCredential authentication failed.. ErrorCode:, Key:Authentication:Twitter:ConsumerAPIKey

内部异常是:

MsalServiceException: AADSTS70002: The client does not exist or is not enabled for consumers

CreateHostBuilder 方法如下所示:

public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder
                    .ConfigureAppConfiguration((hostingContext, config) =>
                    {
                        var settings = config.Build();

                        config.AddAzureAppConfiguration(options =>
                        {
                            options.Connect(settings["ConnectionStrings:AppConfig"])
                                .ConfigureKeyVault(kv =>
                                {
                                    kv.SetCredential(new DefaultAzureCredential());
                                });
                        });
                    })
                    .UseStartup<Startup>();
            });

我在网上发现对此的引用很少,except one post与使用多个凭据有关(我不是)。

任何人都可以给我一个前进的方向:一些关于可能导致它的原因的线索吗?

编辑

以下似乎有效:

var defaultAzureCredentialsOptions = new DefaultAzureCredentialOptions()
{                                
    SharedTokenCacheTenantId = <tenant id>,
    SharedTokenCacheUsername = <my azure username>,
    ExcludeInteractiveBrowserCredential = false,
    ExcludeEnvironmentCredential = false,
    InteractiveBrowserTenantId = <tenant id>
};

config.AddAzureAppConfiguration(options =>
{                                
    options.Connect(settings["ConnectionStrings:AppConfig"])
        .ConfigureKeyVault(kv =>
        {
            kv.SetCredential(new DefaultAzureCredential(defaultAzureCredentialsOptions));
        });
});

虽然这确实有效(就目前而言),但我现在已经硬编码了租户 ID 和我的用户名;当我启动网站时,会出现一个弹出窗口,要求我登录。

最佳答案

DefaultAzureCredential 通过许多凭据,例如推荐用于 Azure 服务的托管身份,因为它更安全(无共享访问 token )。

但是,您可以使用为您的应用程序甚至本地开发期间设置的环境变量,即:

  • AZURE_TENANT_ID:租户 ID
  • AZURE_CLIENT_ID:服务主体 ID,必须已为其授予必要的权限,例如 listget,以便了解如何在示例中使用它们
  • AZURE_CLIENT_SECRET:服务主体 secret (密码),仅在创建后才向您显示

如果您使用 Azure.Identity 的新预览版本,它还支持 Azure CLI、Visual Studio 和其他开发环境凭据。例如,如果您使用 Azure CLI,一旦您 az 登录DefaultAzureCredential 就会正常工作。

关于azure - 从 .Net 应用程序访问 Azure Key Vault - 获取 DefaultAzureCredential 身份验证失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61998175/

相关文章:

android - 无法在 Android 应用程序中从 Microsoft Intune 获取应用程序配置策略

azure - 什么是 Azure 私有(private) DNS 区域组?

c# - 无法导入外部原型(prototype)文件 - 在命令行中工作但在 .net core 3 RC1 中不工作

c# - 使用 Azure Key Vault 的 key 加密和解密文本

.net - 部署到 Azure 应用服务时缺少 Application Insights DLL

c# - Azure 函数通过具有输入数据存储绑定(bind)的队列触发

c# - ASP.NET Core 2.0 JWT 验证失败,出现 `Authorization failed for user: (null)` 错误

c# - 如何阻止从 Controller 继承的类的 OnActionExecuting 中执行操作

node.js - 使用 Node 从 azure keyvault 中提取数据

powershell - Azure key 保管库 : Secret not found error