asp.net - Visual Studio 2019 中多个帐户的 DefaultAzureCredential 异常
问题描述
我遇到了以下场景并最终解决了这个问题:
我有一个 ASP.NET 应用程序,它通过 AzureKeyVaultConfigBuilder 在运行时依赖于 Azure Key Vault。使用 Visual Studio 2019 从我的本地计算机运行,如果仅将我的工作 (@microsoft.com) 帐户添加到 Visual Studio,它就能够访问 Key Vault。但是,如果我将我的个人 Microsoft 帐户 (@live.com) 和我的工作帐户 (@microsoft.com) 添加到 Visual Studio,则应用程序会尝试使用我的个人 Microsoft 帐户 (@live.com) 对 Key Vault 进行身份验证),当它应该使用工作 (@microsoft.com) 帐户时。
两个帐户都登录到 Visual Studio 后,我收到以下异常:
Configuration Error
Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.
Parser Error Message: The configBuilder 'AzureKeyVault' failed while processing the configuration section 'appSettings'.: Error in Configuration Builder 'AzureKeyVault'::GetValue(AzureStorageConnectionString)
Source Error:
Line 32: </builders>
Line 33: </configBuilders>
Line 34: <appSettings configBuilders="AzureKeyVault">
Line 35: <add key="AzureStorageConnectionString" value="" />
Line 36: </appSettings>
Source File: C:\<path to ASP.NET Web project>\web.config Line: 34
Click here to show additional error information:
Exception Details: Azure.Identity.CredentialUnavailableException: DefaultAzureCredential failed to retrieve a token from the included credentials.
EnvironmentCredential authentication unavailable. Environment variables are not fully configured.
ManagedIdentityCredential authentication unavailable, no managed identity endpoint found.
SharedTokenCacheCredential authentication unavailable. Multiple accounts were found in the cache. Use username and tenant id to disambiguate.
解决方案
我经历了很多试错,在环境变量级别、Visual Studio 级别和 Azure CLI 级别设置凭据,但是当在 Visual Studio 中注册多个帐户时,这些都没有得到遵守。
问题出在 Azure.Identity 包上,该包用于针对开发环境的 Azure 对 ASP.NET 应用程序进行身份验证。我安装了 1.1.1 版的软件包。并发调用 DefaultAzureCredential 的多个问题已在 1.2.0 和 1.3.0 版本中得到修复。因此,更新到版本 1.3.0 解决了我的问题并正确遵守了 VisualStudioCredential 的设置。
DefaultAzureCredential指定从各种来源获取不同身份验证令牌的控制流。
推荐阅读
- javascript - 使用返回的 JSON 数据更改状态
- java - 如何仅更新可能已经为空的某些字段(使用 JSON 补丁)
- python-3.x - 如何使用 av.open() 访问我的网络摄像头麦克风中的音频流?
- python - Pandas 使用第一个有效索引按组删除 nan
- python - 计算多个字典键的 Pythonic 方法
- python - 将多行字符串的每一行发送到 for 循环中的列表
- wordpress - Wordpress - 如何让 tag_id 与 post_type 一起使用?
- sql-server - SQL Server 中这两个循环删除查询有什么区别
- r - 在 Ggplot 中添加一个不与先前参数重叠的图层
- javascript - 通过检查响应来处理 Promise 中的错误