c# - dotnet 程序中的 Azure.Identity.CredentialUnavailableException

标签 c# .net azure azure-active-directory azure-keyvault

我最近接手了一个项目,正在尝试在我的 Mac 上启动 dotnet 后端服务器。当我运行应用程序时,我不断收到您在 Program.cs 文件中看到的错误。

Exception has occurred: CLR/Azure.Identity.CredentialUnavailableException
An unhandled exception of type 'Azure.Identity.CredentialUnavailableException' occurred in Microsoft.Extensions.Configuration.AzureAppConfiguration.dll: 'DefaultAzureCredential failed to retrieve a token from the included credentials. See the troubleshooting guide for more information. https://aka.ms/azsdk/net/identity/defaultazurecredential/troubleshoot
- EnvironmentCredential authentication unavailable. Environment variables are not fully configured. See the troubleshooting guide for more information. https://aka.ms/azsdk/net/identity/environmentcredential/troubleshoot
- ManagedIdentityCredential authentication unavailable. Multiple attempts failed to obtain a token from the managed identity endpoint.
- Operating system Darwin 21.6.0 Darwin Kernel Version 21.6.0: Mon Aug 22 20:17:10 PDT 2022; root:xnu-8020.140.49~2/RELEASE_X86_64 isn't supported.
- VisualStudioCodeCredential authentication unavailable. Token acquisition failed. Ensure that you have authenticated in VSCode Azure Account. See the troubleshooting guide for more information. https://aka.ms/azsdk/net/identity/vscodecredential/troubleshoot
- Please run 'az login' to set up account
- Az.Account module >= 2.2.0 is not installed.'
 Inner exceptions found, see $exception in variables window for more details.
 Innermost exception     Azure.Identity.CredentialUnavailableException : EnvironmentCredential authentication unavailable. Environment variables are not fully configured. See the troubleshooting guide for more information. https://aka.ms/azsdk/net/identity/environmentcredential/troubleshoot
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Azure.Identity.CredentialDiagnosticScope.FailWrapAndThrow(Exception ex, String additionalMessage)
   at Azure.Identity.EnvironmentCredential.<GetTokenImplAsync>d__12.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at System.Runtime.CompilerServices.ConfiguredValueTaskAwaitable`1.ConfiguredValueTaskAwaiter.GetResult()
   at Azure.Identity.EnvironmentCredential.<GetTokenAsync>d__11.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at Azure.Identity.DefaultAzureCredential.<GetTokenFromSourcesAsync>d__15.MoveNext()

应用程序使用 azure keyvault 作为环境变量。

我尝试过:

  1. 请求邀请我的 Azure 用户加入该项目。
  2. 我的 Azure 帐户已被授予所有者权利。
  3. 安装了 azure cli 并在 vs code 上使用 az 登录。
  4. 添加了 key 保管库访问控制的数据读取者权限。
  5. 将我的用户添加到与我之前的开发人员具有相同权限的访问策略中。

在运行程序之前,我还确保在 Visual Studio Code 中登录 Azure 帐户。也在 mac os 的常规 Visual Studio 中尝试过,但得到了相同的结果。

最佳答案

请检查您登录的租户是否正确。 为了验证当前登录到 Azure CLI 的帐户。 使用Get-AzContext

enter image description here

  • 从那里检查是否正在使用正确的tenantId或租户。

  • 如果未通过所需的凭据使用正确的凭据登录 属性。

  • 排除除所需之外的所有凭据选项。

  • 使用 ExcludeXXXCredential 属性排除凭据 默认AzureCredentialOptions

示例:

var credential = new DefaultAzureCredential(new DefaultAzureCredentialOptions{
ExcludeEnvironmentCredential = false,
ExcludeManagedIdentityCredential = true,
ExcludeVisualStudioCredential = true,
ExcludeAzureCliCredential = false,
ExcludeAzurePowerShellCredential = true,
ExcludeSharedTokenCacheCredential=true});

Please note that the DefaultAzureCredential and EnvironmentCredential can be able to configured with environment variables which requires specific variables:

  • 默认凭据会查找本地值,因此如果不是 存在,那么它将从 ManagedIdentityCredential 中获取, SharedTokenCacheCredential、InteractiveBrowserCredential 。(排除 没有明确需要什么。)
  • 请确保环境变量已 使用时的 AZURE_CLIENT_ID、AZURE_TENANT_ID、AZURE_CLIENT_SECRET secret 。

引用:Environment variables · GitHub

关于c# - dotnet 程序中的 Azure.Identity.CredentialUnavailableException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73811893/

相关文章:

c# - 绑定(bind)下拉列表而不引起回发

c# - 为什么 Socket.AcceptAsync 没有触发 SocketAsyncEventArgs Completed 事件?

c# - 2 字母 ISO 国家代码到 CultureInfo

javascript - VS.NET : Doing a post-build step in a web project

c# - 异常在 try/catch 上下文中丢失了部分堆栈跟踪

c# - Windows Azure 表存储帐户中如何拥有用户和角色信息以进行系统身份验证

node.js - 连接到 Azure 容器时出错

node.js - 在 Azure 应用服务 (Linux) 上运行的 Node/Next/React 应用程序是否需要 web.config 文件?

c# - 当用户单击标题栏中的十字时,如何显示 MessageBox 提示

.net - 简单的linq to sql没有支持的SQL转换