如何在 Powershell 中授予管理员对 Azure AAD 应用的许可?

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

我正在尝试使用 Azure 活动目录对 Azure Web 应用程序进行身份验证。 到目前为止,我已经采取了以下步骤:

1- 通过 Azure 门户,我在 AAD 中创建了一个应用程序注册,并按照 here 中的说明将其分配给 Web 应用程序。

2- 我使用

New-AzureADGroupAppRoleAssignment cmdlet
将一些用户分配到我的应用程序,然后使用
set-AzureADServicePrincipal -AppRoleAssignmentRequired $true

将用户分配设置为要求

这按预期工作:未分配给该应用程序的用户会看到“拒绝访问”页面,而分配给该应用程序的用户会看到“管理员同意”提示页面。

问题是管理员同意提示中没有链接供他们请求。 我尝试按照 here 中的说明进行操作,但我无法通过门户访问 AAD。我只能通过 Powershell 做到这一点。

如果您知道用于设置此链接(或将管理员同意更改为用户同意)的 Powershell cmdlet,请在此处发布,我将不胜感激。

azure powershell authentication active-directory
5个回答
4
投票

目前在 PowerShell 中没有授予管理员同意的命令,在您的情况下,如果您可以使用 powershell 访问 Azure AD,我认为您也可以通过 Azure CLI 访问它。

所以我的解决方法是在 Azure CLI 中使用

az ad app permission admin-consent
,它相当于门户中的管理员同意按钮。

确保您已经安装了 Azure CLI,使用

az login
登录 AAD 租户中作为管理员的用户帐户或服务主体,然后运行以下命令。

az ad app permission admin-consent --id <application-id>

3
投票

在尝试让它与服务主体一起工作后,事实证明这是不可能的,因为这只能由用户帐户执行

az ad app permission admin-consent --id <application-id>
将在未来的版本中被删除,如本问题线程所讨论的:

https://github.com/Azure/azure-cli/issues/12137

现在向管理员授予 API 许可的建议方法是:

az ad app permission grant --id 46eb4122-bd2b-4f54-af7b-6d79b46ee31a 
                           --api 00000003-0000-0000-c000-000000000000
                           --scope "Directory.Read.All Directory.ReadWrite.All"

Microsoft 文档:az 广告应用程序权限授予


2
投票

你有两个选择。如果您通过门户或 AZ cli 具有全局管理员权限,则可以授予管理员同意。最简单的方法是通过门户。只需转到 AAD,应用程序注册,然后找到您的应用程序。转到权限刀片。您应该会看到一个同意按钮。我不记得我脑海中浮现出的 AZ cli 命令,但在门户中执行它可能更容易。


0
投票

不幸的是,这很棘手。无法与 Azure 门户中的“按下按钮”相提并论。

这是有效的 PowerShell 函数:

using namespace System.Collections.Generic
function Grant-AdminConsentToAllPermissions {
    param(
        [string]$AppDisplayName
    )

    $App = Get-MgApplication -Filter "DisplayName eq '$AppDisplayName'"

    $sp = Get-MgServicePrincipal -Filter "AppId eq '$($App.AppId)'"

    foreach ($resourceAccess in $App.RequiredResourceAccess) {
        $resourceSp = Get-MgServicePrincipal -Filter "AppId eq '$($resourceAccess.ResourceAppId)'"
        if (!$resourceSp) {
            throw "Please cleanup permissions in the Azure portal for the app '$App.AppId', it contains permissions for removed App."
        }
        $scopesIdToValue = @{}
        $resourceSp.PublishedPermissionScopes | % { $scopesIdToValue[$_.Id] = $_.Value }
        [HashSet[string]]$requiredScopes = $resourceAccess.ResourceAccess | % { $scopesIdToValue[$_.Id] }
        $grant = Get-MgOauth2PermissionGrant -Filter "ClientId eq '$($sp.Id)' and ResourceId eq '$($resourceSp.Id)'"
        $newGrantRequired = $true
        if ($grant) {
            [HashSet[string]]$grantedScopes = $grant.Scope.Split(" ")
            if (!$requiredScopes.IsSubsetOf($grantedScopes)) {
                Write-Host "Revoking grant for '$($resourceSp.DisplayName)'"
                Remove-MgOauth2PermissionGrant -OAuth2PermissionGrantId $grant.Id
            }
            else {
                $newGrantRequired = $false
            }
        }
        if ($newGrantRequired) {

            $consentExpiry = ([datetime]::Now.AddYears(10)) 
            $scopesToGrant = $requiredScopes -join " "
            Write-Host "Issuing grant for '$($resourceSp.DisplayName)', scope = $scopesToGrant"
            New-MgOauth2PermissionGrant -ClientId $sp.Id -ConsentType "AllPrincipals" `
                -ResourceId $resourceSp.Id -Scope $scopesToGrant `
                -ExpiryTime $consentExpiry | Out-Null
        }
    }
}

-1
投票

这个 PowerShell 函数(灵感来自 https://f12.hu/2021/01/13/grant-admin-consent-to-an-azuread-application-via-powershell/)完成了这项工作。

ApplicationID参数为应用注册的对象ID。上下文来自 Get-AzContext。

function Set-AdminConsent {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory)]
        [string]$applicationId,
        # The Azure Context]
        [Parameter(Mandatory)]
        [object]$context
    )

    $token = [Microsoft.Azure.Commands.Common.Authentication.AzureSession]::Instance.AuthenticationFactory.Authenticate(
        $context.Account, $context.Environment, $context.Tenant.Id, $null, "Never", $null, "74658136-14ec-4630-ad9b-26e160ff0fc6")
    $headers = @{
        'Authorization'          = 'Bearer ' + $token.AccessToken
        'X-Requested-With'       = 'XMLHttpRequest'
        'x-ms-client-request-id' = [guid]::NewGuid()
        'x-ms-correlation-id'    = [guid]::NewGuid()
    }

    $url = "https://main.iam.ad.ext.azure.com/api/RegisteredApplications/$applicationId/Consent?onBehalfOfAll=true"
    Invoke-RestMethod -Uri $url -Headers $headers -Method POST -ErrorAction Stop
}
© www.soinside.com 2019 - 2024. All rights reserved.