【问题标题】:How to create scope using Azure CLI (az ad app)如何使用 Azure CLI(az ad app)创建范围
【发布时间】:2020-10-21 05:33:32
【问题描述】:

使用 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`

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

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

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

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

【问题讨论】:

    标签: azure azure-active-directory azure-cli2 azure-ad-powershell-v2


    【解决方案1】:

    在上述线程的帮助下,以及大量的试验错误和pretty useful link,我能够制定 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"`
    

    【讨论】:

      【解决方案2】:

      来自这篇文章Azure CLI: Create an Azure AD application for an API that exposes OAuth2 Permissions

      您可以使用az ad app update 命令(see documentation

      然后您可以使用可选参数–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"
          }
        ]
        
      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
        

      【讨论】:

      • 嗨,这看起来很有希望。但是,此行 $DEFAULT_SCOPE=$(az ad app show --id $appId | jq '.oauth2Permissions[0].isEnabled = false' | jq -r '.oauth2Permissions') 因 jq 失败:术语 'jq' 是不被识别为 cmdlet、函数、脚本文件的名称或...。我会看看是否有其他方法。
      • 你是在 windows 还是 linux 上运行? jq 是 bash/shell 的 json 查询包 stedolan.github.io/jq
      • 在 Windows 上运行。我正在寻找一个等价物
      • 如果你使用 powershell ConvertTo-JsonConvertFrom-Json,它应该可以解决问题吗?
      • 如果我知道要更改什么,我是否只是在 OAuthPermissions 中将 IsEnabled 设置为 false,然后将该 JSON(整个内容)传递回集合中?我可以使用以下命令隔离 JSON 块: az ad app list --display-name $appName --query [].oauth2Permissions[0]
      猜你喜欢
      • 2022-10-04
      • 2019-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多