简体   繁体   English

使用 powershell 使用访问令牌连接到 azure 广告

[英]Using powershell to connect to azure ad using access token

I'm writing a powershell script to connect to azure ad using access token.我正在编写一个 powershell 脚本以使用访问令牌连接到 azure 广告。 I'm using below command:我正在使用以下命令:

Connect-AzureAD -TenantId "$tenantId"  -AadAccessToken $tokenResponse.access_token -AccountId "$appId"

I have given application "Contirbute" & "Global Administrator" Role, also Application has all required permissions.我给了应用程序“继续”和“全局管理员”角色,应用程序也具有所有必需的权限。

When I execute the script I can connect to Azure AD but if I try to run subsequent commands after this it gives be below error.当我执行脚本时,我可以连接到 Azure AD 但是如果我尝试在此之后运行后续命令,则会出现以下错误。

Please assist me on this further.请进一步协助我。

Get-AzureADUser : Error occurred while executing GetUsers
Code: Authentication_ExpiredToken
Message: Your access token has expired. Please renew it before submitting the request.
HttpStatusCode: Unauthorized
HttpStatusDescription: Unauthorized
HttpResponseStatus: Completed
At C:\Users\kajalchaudhari\Desktop\test_connect.ps1:22 char:1
+ Get-AzureADUser -Top 10
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [Get-AzureADUser], ApiException
+ FullyQualifiedErrorId : 
Microsoft.Open.AzureAD16.Client.ApiException,Microsoft.Open.AzureAD16.PowerShell.GetUser

seems like you need to configure the token expiry in Azure AD.似乎您需要在 Azure AD 中配置令牌到期。 if you have already done that and the error message is instant then see if following blog is helpful如果您已经这样做并且错误消息是即时的,那么请查看以下博客是否有帮助

https://www.insight.com/en_US/content-and-resources/tech-tutorials/azure-active-directory-authentication-token.html https://www.insight.com/en_US/content-and-resources/tech-tutorials/azure-active-directory-authentication-token.html

The error means the AAD Graph token you passed has expired, you need to pass a new one.该错误表示您传递的 AAD Graph 令牌已过期,您需要传递一个新令牌。

Or if you want to automate this, you could follow the steps below.或者,如果您想自动执行此操作,您可以按照以下步骤操作。

1.First, make sure you have installed the Az module. 1.首先,确保您已经安装了Az模块。 Then get the application id , tenant id , client secret of the AD App which is the global admin.然后获取作为全局管理员的 AD App 的application idtenant idclient secret

2.Then run the script below, it works fine on my side. 2.然后运行下面的脚本,它在我这边工作正常。

$azureAplicationId ="<application id>"
$azureTenantId= "<tenant id>"
$azurePassword = ConvertTo-SecureString "<client secret>" -AsPlainText -Force
$psCred = New-Object System.Management.Automation.PSCredential($azureAplicationId , $azurePassword)
Connect-AzAccount -Credential $psCred -TenantId $azureTenantId -ServicePrincipal

$context = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile.DefaultContext
$aadToken = [Microsoft.Azure.Commands.Common.Authentication.AzureSession]::Instance.AuthenticationFactory.Authenticate($context.Account, $context.Environment, $context.Tenant.Id.ToString(), $null, [Microsoft.Azure.Commands.Common.Authentication.ShowDialog]::Never, $null, "https://graph.windows.net").AccessToken
Connect-AzureAD -AadAccessToken $aadToken -AccountId $context.Account.Id -TenantId $context.tenant.id

Get-AzureADUser

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Powershell获取Azure AD令牌(jwt) - Using Powershell to get Azure AD Token (jwt) 使用Azure App Service中的Powershell命令连接到Azure AD - Connect to Azure AD using Powershell commands from Azure App Service 使用 AD 令牌访问在 Azure AD 中注册的多个应用程序 - Using AD Token for access to multiple applications registered in Azure AD 使用访问令牌通过 Powershell 连接到 Exchange Online - Connect to Exchange Online with Powershell using Access Token Azure SQL 使用 PowerShell 和 ServicePrincipal 为 AD 用户授予访问权限 - Azure SQL Grant Access for AD User using PowerShell and ServicePrincipal 使用 Passport.js 的访问令牌的 Azure AD 无效签名 - Azure AD Invalid Signature of Access Token using Passport.js 如何使用 Azure AD 访问和过滤 JWT 令牌中的组? - How to access and filter groups in JWT token using Azure AD? 使用Azure AD Connect工具 - Using the Azure AD Connect Tool Azure AD Token generaiton using reactjs, to access WebAPI and SharePoint REST API/GrapAPI using jwt token - Azure AD Token generaiton using reactjs, to access WebAPI and SharePoint REST API/GrapAPI using jwt token 使用从 Powershell 中的 AzureRmContext 获取的令牌后,AD 图 API 的访问令牌验证失败 - Access token validation failure for AD graph API after using the token acquired from AzureRmContext in Powershell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM