【问题标题】:Unable to cast TokenCloudCredentials to AccessTokenCredential when calling New-AzureRmADApplication调用 New-AzureRmADApplication 时无法将 TokenCloudCredentials 转换为 AccessTokenCredential
【发布时间】:2016-03-15 06:56:46
【问题描述】:

我正在编写一个 PowerShell 部署脚本,它可以自动创建我的 Azure 资源和随附的 ServicePrincipal。

这是我正在使用的代码,我已经测试并在使用最新的 Azure 1.0.4 SDK 模块直接从 PowerShell 运行时工作:

$ResourceGroupName = "my-resource-group"
$ADAppIdentifierUri = [string]::Concat("https://", $ResourceGroupName, ".azurewebsites.net")

# Generate a password for the AD application
$ServicePrincipalPassword = [Guid]::NewGuid().ToString().Replace("-", "")

# Create the Azure AD Application and service principal, and only assign access to our resource group
$AzureADApplication = New-AzureRmADApplication -DisplayName $ResourceGroupName -HomePage $ADAppIdentifierUri -IdentifierUris $ADAppIdentifierUri -Password $ServicePrincipalPassword

当我在 Visual Studio 中使用我的 ResourceGroup 项目部署脚本运行此代码时,我收到以下错误:

New-AzureRmADApplication:无法将“Microsoft.Azure.TokenCloudCredentials”类型的对象转换为“Microsoft.Azure.Common.Authentication.AccessTokenCredential”类型。

根据堆栈跟踪,异常是在命令 New-AzureRmADApplication 开始时引发的,因此很遗憾,异常是在 Azure SDK 代码内部发生的。

我在以下文件中浏览了 SDK 的源代码,但没有找到任何见解:

https://github.com/Azure/azure-powershell/blob/f803b991daa7eeeea1217238ab071c8d83de34be/src/ResourceManager/Resources/Commands.Resources/ActiveDirectory/NewAzureADApplicationCommand.cs

https://github.com/Azure/azure-powershell/blob/956d0ca795acfce67d8f142bf059ab2b8ab2c67b/src/ResourceManager/Resources/Commands.Resources/Models.ActiveDirectory/ActiveDirectoryClient.cs

https://www.symbolsource.org/Public/Metadata/NuGet/Project/Microsoft.Azure.Graph.RBAC/1.6.0-preview/Release/.NETFramework,Version%3Dv4.0/Microsoft.Azure.Graph.RBAC/Microsoft.Azure.Graph.RBAC/Generated/GraphRbacManagementClient.cs?ImageName=Microsoft.Azure.Graph.RBAC

我只能在此链接中找到一个遇到同样错误的人: https://azure.microsoft.com/en-us/documentation/articles/resource-group-authenticate-service-principal/

但是,那里的解决方案对我没有意义,因为我没有使用管理证书进行身份验证,并且在 manage.windowsazure.com 站点上没有列出任何管理证书。

【问题讨论】:

  • 您使用的是哪个版本的 Visual Studio?我复制了您的代码并使用Login-AzureRmAccount 运行,它对我来说非常有效。我正在使用 Visual Studio 2015 和 Azure PowerShell 1.2.1
  • 我使用的是相同的 Visual Studio 2015 和 Azure PowerShell - 2016 年 2 月 (1.2.1)。
  • 你使用什么样的登录策略? Visual Studio 自动生成的脚本使用 Microsoft.Azure.Common.Authentication.AzureSession,并且该脚本使用 Azure PowerShell 0.9 样式的命令,它不适用于 Azure PowerShell 1.2.1。我想弄清楚如何使用 Visual Studio 的 Azure Session 登录。如果您分享您的代码,将会非常有帮助。
  • 我正在使用项目模板 Cloud -> Resource Group 附带的样板 Deploy-AzureResourceGroup.ps1 脚本。我正在使用项目文件的实际部署选项运行脚本。我上面链接的代码的sn-p可以放在这个文件里测试一下情况。
  • 通过使用 Get-AzureRmContext cmdlet,我能够确定在 Visual Studio 中运行 Azure PowerShell 时,帐户的身份验证是使用 AccessToken 完成的。但是,当我在 PowerShell 中使用 Login-AzureRmAccount 进行身份验证时,它不使用这样的令牌。我猜这是导致潜在问题的原因。

标签: azure azure-powershell azure-resource-manager service-principal


【解决方案1】:

在 AzureRMAD* cmdlet 使用基于令牌的身份验证时,这是一个问题(即错误)。当您从 VS 运行脚本时,VS 使用您从 VS 登录获得的令牌以避免提示进行身份验证。要解决它,您必须使用凭据在 VS 之外运行它。

有一个内部工作项跟踪此问题,但如果您想监控进度,您可以在此处提交问题:https://github.com/Azure/azure-powershell/issues/

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-03
  • 1970-01-01
  • 2016-06-08
相关资源
最近更新 更多