如何使用 Azure CLI 创建新应用程序后设置 Azure 应用程序范围

问题描述 投票:0回答:1

我正在尝试创建一个应用程序并尝试为其创建范围,我能够创建该应用程序,但无法创建范围。我已经厌倦了寻找解决方案,我找到了这个链接,但它似乎已经过时了。

这是创建应用程序的脚本:

$appId = az ad app create --display-name "webapp - dev" --sign-in-audience "AzureADMyOrg" --required-resource-accesses "./appregistration/script.json" --query appId -o tsv

我有一个名为 Permissions.json 的文件,用于范围。

[
{
    "adminConsentDescription": "Allow the app to access Api endpoints",
    "adminConsentDisplayName": "webApi",
    "id": null,
    "isEnabled": true,
    "type": "Admin",
    "userConsentDescription": null,
    "userConsentDisplayName": null,
    "value": "webApi"
}

]

我正在尝试从此文件中获取数据并创建范围。这是一个脚本。

 $json = Get-Content './appregistration/permissions.json' | Out-String | ConvertFrom-Json

foreach ($element in $json)
{$element.id = [guid]::NewGuid() }
 $apiScopeJson = @{oauth2PermissionScopes = $json}
 az ad app update --id $appId --set api=$apiScopeJson

我收到此错误消息。

有效负载中的属性 api 的值与架构不匹配。

azure azure-active-directory azure-cli azure-cli2 azure-ad-powershell-v2
1个回答
0
投票

我使用了@A2AdminGuy的类似代码,来自您在问题中提到的相同链接,并且能够成功向应用程序添加新范围:

$AppId = az ad app create --display-name "webapp - dev" --sign-in-audience "AzureADMyOrg" --query appId -o tsv
Start-Sleep -Seconds 60
$scopeGUID = [guid]::NewGuid()
$permission = @{
    adminConsentDescription="Allow the app to access Api endpoints"
    adminConsentDisplayName="webApi" 
    id="$scopeGUID"
    isEnabled=$true
    type="Admin"
    userConsentDescription="null"
    userConsentDisplayName="null"
    value="webApi"
}
$AppObjId = (az ad app show --id $AppId | ConvertFrom-JSON).id

$appIduri = az ad app update --id $AppObjId --identifier-uris api://$AppId 


$accesstoken = (Get-AzAccessToken -Resource "https://graph.microsoft.com/").Token
$header = @{
    'Content-Type' = 'application/json'
    'Authorization' = 'Bearer ' + $accesstoken
}
$bodyaccess = @{
    'api' = @{
        'oauth2PermissionScopes' = @($permission)
    }
}|ConvertTo-Json -d 3

Invoke-RestMethod -Method Patch -Headers $header -Uri "https://graph.microsoft.com/v1.0/applications/$AppObjId" -Body $bodyaccess

PowerShell 输出:

enter image description here

当我在门户中检查相同内容时,使用新范围创建了新的应用程序,如下所示:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.