如何在Powershell中授予Azure AAD应用程序管理员同意



我正在尝试使用Azure Active directory验证Azure Web应用程序。到目前为止,我已经采取了以下步骤:

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

2-我使用New-AzureADGroupAppRoleAssignment cmdlet为我的应用程序分配了一些用户,然后使用set-AzureADServicePrincipal -AppRoleAssignmentRequired $true将用户分配设置为一项要求

这正如预期的那样:未分配给该应用程序的用户,可以看到一个";访问被拒绝";页面,以及那些是的人,看到";行政同意";。

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

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

目前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>

在尝试将其与服务主体一起使用后,事实证明这是不可能的,因为这只能由用户帐户执行。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"

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

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

不幸的是,这很棘手。不能与";按下按钮";在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
}
}
}

此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
}

最新更新