最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

azure - Access Token For User managed identity configuration - Stack Overflow

programmeradmin1浏览0评论

I PowerShell based scripts to update the configuration of azure resources , using Connect-AzAccount -Identity -AccountId $env:userid -WarningAction Ignore this way I am connecting the portal through user assigned managed identity , now I want create the access token to update the resources configuration by rest api method like invoking the uri. previously i had below method to create the access token $response = Invoke-WebRequest -Uri 'http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https%3A%2F%2Fmanagement.azure%2F' -Headers @{Metadata="true"} this will give the token since it was system assigned managed identity it worked . how to create access token if we connect the portal through user assigned identity ?

I PowerShell based scripts to update the configuration of azure resources , using Connect-AzAccount -Identity -AccountId $env:userid -WarningAction Ignore this way I am connecting the portal through user assigned managed identity , now I want create the access token to update the resources configuration by rest api method like invoking the uri. previously i had below method to create the access token $response = Invoke-WebRequest -Uri 'http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https%3A%2F%2Fmanagement.azure%2F' -Headers @{Metadata="true"} this will give the token since it was system assigned managed identity it worked . how to create access token if we connect the portal through user assigned identity ?

Share Improve this question asked Mar 14 at 21:30 pkspks 517 bronze badges 1
  • Could you confirm whether your requirement is to generate access token with user assigned managed identity? – Sridevi Commented Mar 15 at 3:33
Add a comment  | 

1 Answer 1

Reset to default 1

To get access token using User assigned Managed Identity, you need to include either client_id or object_id in the PowerShell script.

Initially, I created one user-assigned managed identity named usermid15 like this:

Make sure to add this managed identity to the Azure resource from which you want to generate access token. In my case, I added it to Azure Virtual Machine:

Now, I ran below PowerShell script in Azure VM and got access token successfully with user-assigned managed identity client ID:

$clientId = "user_managed_client_Id"
$response = Invoke-WebRequest -Uri "http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https%3A%2F%2Fmanagement.azure%2F&client_id=$clientId" -Headers @{Metadata="true"}
$accessToken = ($response.Content | ConvertFrom-Json).access_token
Write-Output "Access Token: $accessToken"

Response:

发布评论

评论列表(0)

  1. 暂无评论