[英]How do I add a scope in AD B2C App Registration using AZ CLI
[英]How to create scope using Azure CLI (az ad app)
使用 Azure CLI 2.x,我找不到在 Azure AD 門戶中公開 API 部分下“添加范圍”的方法。
我看到的是,如果我在創建應用程序時傳遞 --identifier-uris,APP ID URI 和 Scope 會自動設置:
`az ad app create --display-name "$appName" --identifier-uris "https://$tenantDomain/$appName" --reply-urls "$replyUrl" --oauth2-allow-implicit-flow true`
不是我所期望的,也不是我想要的
因此,我從創建命令中刪除了 --identifier-urls 並手動添加了我想要的 scope。 然后我通過清單看到我在 OAuth2Permissions 下尋找的內容,如下所示。 我可以用新的 guid 將它放在 manifest.json 中並以某種方式插入嗎?
什么 CLI 命令支持顯式支持定義一個 Scope? 然后添加一個客戶端應用程序,我需要 select 定義的 Scope,這是如何引用的?
IMO,文檔非常稀少。 這個參考非常有用,但這里沒有任何內容談論添加范圍和客戶端。 https://learn.microsoft.com/en-us/cli/azure/ad?view=azure-cli-latest 。 非常感謝對示例或文檔的任何幫助。
從這篇文章Azure CLI:為公開 OAuth2 權限的 API 創建 Azure AD 應用程序
您可以使用az ad app update
命令( 請參閱文檔)
然后,您可以使用可選參數–set
設置應用程序的屬性
創建一個oauth2-permissions.json
包含權限:
[ { "adminConsentDescription": "Access CP Debug Desc", "adminConsentDisplayName": "Access CP Debug", "id": "85b8f1a0-0733-47dd-9af4-cb7221dbcb73", "isEnabled": true, "type": "Admin", "userConsentDescription": null, "userConsentDisplayName": null, "value": "Access" } ]
運行此腳本,它將創建應用程序,禁用現有的 scope 並添加新的 scope:
# Create the app registration APP_REG=$(az ad app create --display-name myapi --identifier-uris https://myapi) # Get the app id APP_ID=$(echo $APP_REG | jq -r '.appId') # disable default exposed scope DEFAULT_SCOPE=$(az ad app show --id $APP_ID | jq '.oauth2Permissions[0].isEnabled = false' | jq -r '.oauth2Permissions') az ad app update --id $APP_ID --set oauth2Permissions="$DEFAULT_SCOPE" # Create new scopes from file 'oath2-permissions' az ad app update --id $APP_ID --set oauth2Permissions=@oauth2-permissions.json
在上述線程的幫助下,以及大量的試驗錯誤加上一個非常有用的鏈接,我能夠使用 windows 環境制定 CLI 腳本來添加 scope。 PowerShell 對 windows 上的“jq”不滿意,必須刪除反勾號才能使事情正常工作。 現在我需要解決使用 CLI 添加 preAuthorizedApplication 的問題。
$userAccessScopeApi = '{
"lang": null,
"origin": "Application",
"adminConsentDescription": "Access CP Debug desc",
"adminConsentDisplayName": "Access CP Debug",
"id": "--- replaced in scripts ---",
"isEnabled": true,
"type": "Admin",
"userConsentDescription": null,
"userConsentDisplayName": null,
"value": "Access"
}' | ConvertTo-Json | ConvertFrom-Json
`
Write-Host " - 1 read oauth2permissions"
#(az ad app show --id $appid)
$appjson = (az ad app list --display-name $appName)
$app = $appjson | ConvertFrom-Json
$oauth2Permissions = $app.oauth2Permissions
$oauth2Permissions[0].isEnabled = 'false'
$oauth2Permissionsjson = ConvertTo-Json -InputObject @($oauth2Permissions)
Write-Host " - 2 disable oauth2Permission in Azure App Registration"
$oauth2Permissionsjson | Out-File -FilePath .\oauth2Permissionsold.json
az ad app update --id $appId --set oauth2Permissions=@oauth2Permissionsold.json
Write-Host " - 3 delete the default oauth2Permission"
az ad app update --id $appId --set oauth2Permissions='[]'
Write-Host " - 4 add the new scope required add the new oauth2Permissions values"
$oauth2PermissionsApiNew = $userAccessScopeApi | ConvertFrom-Json
$oauth2PermissionsApiNew[0].id = New-Guid
$oauth2PermissionsApiNew = ConvertTo-Json -InputObject @($oauth2PermissionsApiNew)
# Write-Host "new oauth2permissions : " + $oauth2PermissionsApiNew"
$oauth2PermissionsApiNew | Out-File -FilePath .\oauth2Permissionsnew.json
az ad app update --id $appId --set oauth2Permissions=@oauth2Permissionsnew.json
Write-Host " - Updated scopes (oauth2Permissions) for App Registration: $appId"`
正如A2AdminGuy提到的,無法再直接更新oauth2Permissions
,但您仍然可以使用az ad app update
。
az ad app show --id $appClientId
你會看到api
現在在oauth2PermissionScopes
里面:"api": {
"acceptMappedClaims": null,
"knownClientApplications": [],
"oauth2PermissionScopes": [],
"preAuthorizedApplications": [],
"requestedAccessTokenVersion": null
}
api
屬性才能設置oauth2PermissionScopes
:$apiScopeId = [guid]::NewGuid().Guid
$apiScopeJson = @{
requestedAccessTokenVersion = 2
oauth2PermissionScopes = @(
@{
adminConsentDescription = "$AppName on $EnvironmentAbbrev"
adminConsentDisplayName = "$AppName on $EnvironmentAbbrev"
id = "$apiScopeId"
isEnabled = $true
type = "User"
userConsentDescription = "$AppName on $EnvironmentAbbrev"
userConsentDisplayName = "$AppName on $EnvironmentAbbrev"
value = "authenticate"
}
)
} | ConvertTo-Json -d 4 -Compress
$apiUpdateBody = $apiScopeJson | ConvertTo-Json -d 4
az ad app update --id $apiClientId --set api=$apiUpdateBody --verbose
--verbose
參數可以去掉,但有趣的是,update 命令是對圖 API 的 PATH 請求。
由於是 PATH 請求,你不必總是發送整個api
屬性,你可以做兩個請求來更新不同的屬性,而前一個不會受到影響。
redirectUris
的方法:$appSpaJson = @{
redirectUris = @("http://localhost:3000")
} | ConvertTo-Json -d 3 -Compress
$appUpdateBody = $appSpaJson | ConvertTo-Json -d 4
az ad app update --id $appClientId --set spa=$appUpdateBody
截至 22 年 7 月 29 日,最新的 Azure CLI 命令“az ad app update”不再包含 oauth2permissions。 如果您嘗試上述方法,您會大吃一驚,並希望找到這篇文章。 這些權限在應用程序注冊表上的新位置位於 api.oauth2PermissionScopes 作為數組。
為了解決這個問題,我結合了這篇文章中的一些項目,並且必須發揮創意,因為Azure 文檔仍然不正確。
如果你有一個現有的 API 暴露,你必須禁用它才能修改范圍,這仍然是正確的。 如果您有一個新的應用程序注冊,您可以直接應用此應用程序而不會出現問題。 希望這可以幫助像我這樣的人,由於應用程序注冊方式的變化和清單的變化,自動化現在被破壞了。 如果您不了解應用注冊的更改,我建議您查看。 如果你做到了這一點,我想你已經做到了。
# Add API Read Scope:
$scopeGUID = [guid]::NewGuid()
$scopeJSONHash = @{
adminConsentDescription="$apiName on $svrAppRegName"
adminConsentDisplayName="$apiName on $svrAppRegName"
id="$scopeGUID"
isEnabled=$true
type="User"
userConsentDescription="$apiName on $svrAppRegName"
userConsentDisplayName="$apiName on $svrAppRegName"
value="$apiName"
}
$azAppOID = (az ad app show --id $serverApplicationId | ConvertFrom-JSON).id
$accesstoken = (Get-AzAccessToken -Resource "https://graph.microsoft.com/").Token
$header = @{
'Content-Type' = 'application/json'
'Authorization' = 'Bearer ' + $accesstoken
}
$bodyAPIAccess = @{
'api' = @{
'oauth2PermissionScopes' = @($scopeJSONHash)
}
}|ConvertTo-Json -d 3
#You can try az rest, I used Invoke-RestMethod though.
#$graphURL="https://graph.microsoft.com/v1.0/applications/$azAppOID"
#az rest --method PATCH --uri $graphurl --headers $header --body $bodyAPIAccess
Invoke-RestMethod -Method Patch -Uri "https://graph.microsoft.com/v1.0/applications/$azAppOID" -Headers $header -Body $bodyAPIAccess
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.