Azure Keyvault error: Tried the following 3 methods to get an access token, but none of them worked

Azure Keyvault error: Tried the following 3 methods to get an access token, but none of them worked

我已将我的 WebAPI 配置为使用托管标识从 Azure KeyVault 中获取 Azure 存储连接字符串。

为此,我创建了一个 Azure AD 专用用户,该用户有权访问来自 Azure KeyVault 的 get/list 机密。然后我将此用户分配给我的 AZURE_USERNAME 环境变量,并且我已配置 Visual Studio 使用此帐户。

当我从 Visual Studio 调试时,只要我 运行 IIS Express 中的 WebAPI 一切正常。但是,当我在 Visuall Studio 中进行调试时,我想 运行 来自本地 IIS 的 WebApi。

我在这里错过了什么?我怀疑这是因为本地 IIS 不使用上面配置的用户 运行,但解决方法是什么?

我得到的错误是

AzureServiceTokenProviderException: Parameters: Connection String: [No connection string specified], Resource: https://vault.azure.net, Authority: https://login.windows.net/some_id. Exception Message: Tried the following 3 methods to get an access token, but none of them worked.

我在这方面取得了进展,它现在可以在本地 IIS 中运行,但是...

为了让它工作,我添加了以下环境变量:

作为documented here

然而,这意味着这一次仅适用于一个服务主体。如果我想在本地 IIS 中同时(就是这种情况)我所有的本地 WebApi 运行 怎么办。我该如何处理 DefaultAzureCredential ?

最后,我在 azure ad 中创建了一个专用服务主体,然后将其添加到我的应用程序的用户中。此服务主体由开发机器上的环境变量引用。

另一种解决方案是通过传递将指定租户和应用程序机密的连接字符串参数来创建 AzureServiceTokenProvider 实例。

var azureServiceTokenProvider = new AzureServiceTokenProvider("RunAs=App;AppId=2ad1fe1f-d93f-4973-a1e5-e7019638b90a;TenantId=your-tenant-here.onmicrosoft.com;AppKey=your-app-secret-here");

这样就不用再使用环境变量了

检查一下here