如何使用 Azure CLI 创建范围(az ad app)

use*_*078 2 azure azure-active-directory azure-cli2 azure-ad-powershell-v2

使用 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`
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

不是我所期望的,也不是我想要的

因此,我从 create 命令中删除了 --identifier-urls 并手动添加了我想要的范围。然后我通过清单看到我在 OAuth2Permissions 下寻找的内容,如下所示。我可以用新的 guid 将它放在 manifest.json 中并以某种方式插入吗?

在此处输入图片说明

什么 CLI 命令支持明确支持定义范围?然后添加一个客户端应用程序我需要选择定义的范围,这是如何引用的?

IMO,文档非常稀少。这个参考非常有用,但这里没有讨论添加范围和客户端。https://docs.microsoft.com/en-us/cli/azure/ad?view=azure-cli-latest。非常感谢对示例或文档的任何帮助。

A2A*_*Guy 10

截至 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
Run Code Online (Sandbox Code Playgroud)


小智 6

正如A2AdminGuy提到的,无法oauth2Permissions再直接更新,但您仍然可以使用az ad app update.

  1. 执行命令az ad app show --id $appClientId,你会看到nowoauth2PermissionScopes里面有api
"api": {
    "acceptMappedClaims": null,
    "knownClientApplications": [],
    "oauth2PermissionScopes": [],
    "preAuthorizedApplications": [],
    "requestedAccessTokenVersion": null
}
Run Code Online (Sandbox Code Playgroud)
  1. 您需要更新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
Run Code Online (Sandbox Code Playgroud)

--verbose参数可以删除,但有趣的是,更新命令是对图形 API 的 PATH 请求。

由于它是 PATH 请求,因此您并不总是需要发送整个api属性,您可以执行两个请求来更新不同的属性,前一个请求不会受到影响。

  1. 这是更新 SPA 的方法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
Run Code Online (Sandbox Code Playgroud)


Tho*_*mas 5

来自这篇文章Azure CLI:为公开 OAuth2 权限的 API 创建 Azure AD 应用程序

您可以使用该az ad app update命令(请参阅文档

然后,您可以使用可选参数设置应用程序的属性 –set

  1. 创建一个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"
      }
    ]
    
    Run Code Online (Sandbox Code Playgroud)
  2. 运行此脚本,它将创建应用程序,禁用现有范围并添加新范围:

    # 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
    
    Run Code Online (Sandbox Code Playgroud)


use*_*078 2

在上面线程的帮助下,以及大量的反复试验加上一个非常有用的链接,我能够编写 CLI 脚本来使用 Windows 环境添加范围。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"`
Run Code Online (Sandbox Code Playgroud)