无法在调用New-AzureRmADApplication时将TokenCloudCredentials强制转换为AccessTokenCredential

问题描述:

我正在编写一个PowerShell部署脚本,它可以自动创建我的Azure资源和附带的ServicePrincipal。无法在调用New-AzureRmADApplication时将TokenCloudCredentials强制转换为AccessTokenCredential

这里是我使用的代码,我已经测试和工程时,直接从PowerShell将使用最新的Azure的1.0.4 SDK模块运行:

$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中,我得到以下错误:

New-AzureRmADApplication : Unable to cast object of type 'Microsoft.Azure.TokenCloudCredentials' to type 'Microsoft.Azure.Common.Authentication.AccessTokenCredential'.

根据堆栈跟踪异常在命令新建-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站点上列出任何管理证书。

+0

Visual Studio中的哪个版本?我复制你的代码并使用'Login-AzureRmAccount'运行,它对我来说非常合适。我正在使用Visual Studio 2015和Azure PowerShell 1.2.1 –

+0

我正在使用与Azure PowerShell相同的Visual Studio 2015 - 2016年2月(1.2.1)。 –

+0

你使用什么样的登录策略? Visual Studio自动生成的脚本使用'Microsoft.Azure.Common.Authentication.AzureSession',该脚本使用Azure PowerShell 0.9样式命令,该命令不适用于Azure PowerShell 1.2.1。我想弄清楚如何使用Visual Studio的Azure会话进行登录。如果你分享你的代码,这将是非常有帮助的。 –

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

有一个内部工作项跟踪这一点,但如果你要监控进展情况,你可以在这里提交一个问题:您正在使用https://github.com/Azure/azure-powershell/issues/

+0

是的,我想到了很多,我采用从PowerShell手动调用部署脚本的方法,现在一切正常。 –

+0

我按照建议添加了该问题https://github.com/Azure/azure-powershell/issues/2273 – antmeehan