问题描述
我有一个场景,我需要将应用程序从 DevOps 管道添加到安全组。我有以下工作正常的场景:
- 在管道中,我有以下 powershel 脚本:
if (!((Get-AzADGroupMember -ObjectId ((Get-AzADGroup -displayName $groupName).id)).displayName -eq $appName)) {Add-AzADGroupMember -MemberObjectId (Get-AzADServicePrincipal -displayName $appName).id -TargetGroupObjectId (Get-AzADGroup -displayName $groupName).id} else {"member is already part of the group"}
- 服务主体拥有 Azure Active Directory Graph 的 API 权限和 Directory.Read.All 权限:
- 服务主体是安全组的所有者:
问题是 Azure Active Directory Graph 处于弃用路径,因此我将权限更改为推荐的 Microsoft Graph 权限:
但现在我收到“权限不足,无法完成操作”。错误
请有人建议我还需要配置什么才能使其正常工作吗?
谢谢。
解决方法
虽然 AAD Graph 处于弃用路径,但 MS Graph 和 AAD Graph 的权限不能混淆,它们是不一样的。
可以使用fiddler4捕获Powershell Az cmdlet的请求,发现它其实在调用AAD Graph而不是底部的MS Graph。
当我们使用访问令牌调用官方API时,API需要验证访问令牌是否有效。
有一个名为 aud
的声明,表示您正在调用的资源。当您在 Azure AD 应用程序中分配 MS Graph 权限(例如:https://graph.microsoft.com/user.read
),但您调用的资源是 AAD Graph https://graph.windows.net/
时,MS Graph 权限肯定不会包含在访问令牌中.而此时所需的权限应该是https://graph.windows.net/user.read
。这就是您收到错误权限不足,无法完成操作的原因。
因此在这种情况下,您应该继续使用 AAD Graph 权限。
不要担心 AAD Graph 的停用。在那一天之前,MS 应该能够提供从 AAD Graph 到 MS Graph 的迁移或其他方式,使其仍然可以正常工作,而无需用户做太多事情。