将Azure Active Directory预授权分配给启用MSI的Azure功能的权限不足?

将Azure Active Directory预授权分配给启用MSI的Azure功能的权限不足?,azure,azure-active-directory,azure-functions,azure-api-management,azure-managed-identity,Azure,Azure Active Directory,Azure Functions,Azure Api Management,Azure Managed Identity,我们在同一目录中有两个azure资源。Azure API管理和Azure函数背后的一组webAPI API。我们希望azure函数能够调用API。我们已经在azure功能上启用了MSI,如中所述。我们已经在AAD中为API创建了应用程序注册,并创建了要访问的角色权限。以下是尝试将权限/角色分配给azure功能时遇到的错误: 在powershell中: New-AzureADServiceAppRoleAssignment -ObjectId 8XXXXXX-XXXX-XXXX-XXXX-XXXX

我们在同一目录中有两个azure资源。Azure API管理和Azure函数背后的一组webAPI API。我们希望azure函数能够调用API。我们已经在azure功能上启用了MSI,如中所述。我们已经在AAD中为API创建了应用程序注册,并创建了要访问的角色权限。以下是尝试将权限/角色分配给azure功能时遇到的错误: 在powershell中:

New-AzureADServiceAppRoleAssignment -ObjectId 8XXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX -Id 3XXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX -PrincipalId 8XXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX -ResourceId 9XXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX

New-AzureADServiceAppRoleAssignment : Error occurred while executing NewServicePrincipalAppRoleAssignment
Code: Authorization_RequestDenied
Message: Insufficient privileges to complete the operation.
HttpStatusCode: Forbidden
HttpStatusDescription: Forbidden
HttpResponseStatus: Completed
At line:1 char:1
+ New-AzureADServiceAppRoleAssignment -ObjectId 8XXXXXX-XXXX-XXXX-XXXX ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [New-AzureADServiceAppRoleAssignment], ApiException
    + FullyQualifiedErrorId : Microsoft.Open.AzureAD16.Client.ApiException,Microsoft.Open.AzureAD16.PowerShell.NewServ
   icePrincipalAppRoleAssignment

是给我们一个权限错误,即使是AAD管理员(我想是AAD DC管理员的成员)运行它。以前有人碰到过这个吗?为什么会抛出权限错误?我们已经对3个不同的人验证了ID是否正确。

您可能面临的问题是,尽管将应用注册命名为与启用MSI的应用相同的名称,但这两个名称最终在AAD中表示不同的服务主体

尝试改用MSI标识的对象id运行powershell命令。我能够让它工作,并授予我的MSI应用程序访问Graph Api的权限

以下是我用来分配GraphApi角色的PS,我的功能应用程序需要:

$functionAppName = "My-FANCY-FUNC"

$context = Get-AzureRmContext -ErrorAction SilentlyContinue #this lets you search AAD for func

if(!$context){
    $login = Connect-AzureRmAccount  | Out-Null
    Connect-AzureAD #needed this for Graph API
    $context = $login
} else { Write-Host "Login session already established for " $context.Subscription.SubscriptionName }

#get the SP associated with the MSI
$MSIPrincipal = Get-AzureRmADServicePrincipal -SearchString $functionAppName | Where-Object DisplayName -eq $functionAppName

#get the SP associatesd with the MS Graph
$graph = Get-AzureADServicePrincipal -All $true | ? { $_.DisplayName -match "Microsoft Graph" }

#find the target app roles in the graph
$targetRoles = $graph.AppRoles | Where-Object Value -in "Group.ReadWrite.All", "Directory.ReadWrite.All"

#iterate throgh the known roles and add the MSI SP to them
$targetRoles | ForEach-Object {New-AzureADServiceAppRoleAssignment -Id $_.Id -PrincipalId $MSIPrincipal.Id -ObjectId $MSIPrincipal.Id -ResourceId $graph.ObjectId}
根据您的问题,我怀疑此行将返回多个实体:

Get-AzureRmADServicePrincipal -SearchString $functionAppName | Where-Object DisplayName -eq $functionAppName

删除无关的应用程序注册将清除此问题

谢谢您的回复。不幸的是,我正在传递您指示的值:New AzureADServiceAppRoleAssignment-ObjectId AzureFunction(MSIPrincipal)-Id AppRole(在应用程序注册清单中创建的应用程序角色Id)-PrincipalId AzureFunction(MSIPrincipal)-ResourceId AzureAPI(AAD Principal)只是为了澄清,唯一的应用程序注册是针对API的。该函数仅通过MSI注册。能否共享PS脚本的验证部分?您使用的是Connect AzureAD还是Connect AzurerAccount,或者两者都使用?老实说,没有脚本。我们必须这样做一次,所以我们手动获得了我上面提到的新AzureADServiceAppRoleAssignment调用所需的ID。如果您只是在控制台中键入它,它将无法工作。您需要根据AAD和Azure对会话进行身份验证。我提供的脚本将起作用,或者将帮助您了解用户缺乏权限的地方