簡體   English   中英

使用 powershell 使用訪問令牌連接到 azure 廣告

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

我正在編寫一個 powershell 腳本以使用訪問令牌連接到 azure 廣告。 我正在使用以下命令:

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

我給了應用程序“繼續”和“全局管理員”角色,應用程序也具有所有必需的權限。

當我執行腳本時,我可以連接到 Azure AD 但是如果我嘗試在此之后運行后續命令,則會出現以下錯誤。

請進一步協助我。

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

似乎您需要在 Azure AD 中配置令牌到期。 如果您已經這樣做並且錯誤消息是即時的,那么請查看以下博客是否有幫助

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

該錯誤表示您傳遞的 AAD Graph 令牌已過期,您需要傳遞一個新令牌。

或者,如果您想自動執行此操作,您可以按照以下步驟操作。

1.首先,確保您已經安裝了Az模塊。 然后獲取作為全局管理員的 AD App 的application idtenant idclient secret

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM