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

Unable to cast TokenCloudCredentials to AccessTokenCredential when calling New-AzureRmADApplication

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

这是我正在使用的代码,当 运行 直接从 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 中的 ResourceGroup 项目部署脚本时,出现以下错误:

New-AzureRmADApplication : Unable to cast object of type 'Microsoft.Azure.TokenCloudCredentials' to type '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

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

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

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

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