创建自定义角色以管理 Microsoft Entra ID 中的企业应用

本文介绍如何创建一个在 Microsoft Entra ID 中有权限管理用户和组的企业应用分配的自定义角色。 有关角色分配的元素,以及子类型、权限和属性集等术语的含义,请参阅自定义角色概述

先决条件

  • Microsoft Entra ID P1 或 P2 许可证
  • 特权角色管理员或全局管理员
  • 使用 PowerShell 时安装的 Microsoft Graph PowerShell SDK

有关详细信息,请参阅使用 PowerShell 的先决条件

企业应用角色权限

本文中讨论了两种企业应用权限。 所有示例都使用 update 权限。

  • 若要读取范围内的用户和组分配,请授予 microsoft.directory/servicePrincipals/appRoleAssignedTo/read 权限
  • 若要管理范围内的用户和组分配,请授予 microsoft.directory/servicePrincipals/appRoleAssignedTo/update 权限

授予 update 权限后,被分派人能够管理向企业应用的用户和组分配。 可以针对单个应用程序或针对所有应用程序授予用户和/或组分配的范围。 如果在组织范围级别授予,则被分派人可以管理所有应用程序的分配。 如果在应用程序级别授予,则被分派人只能管理指定应用程序的分配。

授予 update 权限的操作分为两步:

  1. 使用权限 microsoft.directory/servicePrincipals/appRoleAssignedTo/update 创建自定义角色
  2. 向用户或组授予权限,以管理向企业应用的用户和组分配。 这时,你可以将范围设置为组织范围级别或单个应用程序。

Microsoft Entra 管理中心

创建新的自定义角色

提示

本文中的步骤可能因开始使用的门户而略有不同。

注意

自定义角色是在组织范围级别创建和管理的,只在组织的“概述”页中提供。

  1. 至少以特权角色管理员身份登录到 Microsoft Entra 管理中心

  2. 浏览到“标识”>“角色和管理员”>“角色和管理员”

  3. 选择“新建自定义角色”。

    Add a new custom role from the roles list in Microsoft Entra ID

  4. 在“基础”选项卡上,提供“管理用户和组分配”作为角色的名称,提供“授予权限以管理用户和组分配”作为角色说明,然后选择“下一步” 。

    Provide a name and description for the custom role

  5. 在“权限”选项卡上的搜索框中输入“microsoft.directory/servicePrincipals/appRoleAssignedTo/update”,选中所需权限旁边的复选框,然后选择“下一步” 。

    Add the permissions to the custom role

  6. 在“查看 + 创建”选项卡上查看权限,然后选择“创建” 。

    Now you can create the custom role

使用 Microsoft Entra 管理中心将角色分配给用户

  1. 至少以特权角色管理员身份登录到 Microsoft Entra 管理中心

  2. 浏览到“标识”>“角色和管理员”>“角色和管理员”。

  3. 选择“管理用户和组分配”角色。

    Open Roles and Administrators and search for the custom role

  4. 选择“添加分配”,选择所需的用户,然后单击“选择”以向用户添加角色分配 。

    Add an assignment for the custom role to the user

分配提示

  • 若要向被分派人授予权限以管理组织范围内所有企业应用的用户和组访问权限,请从组织的 Microsoft Entra ID“概述”页上组织范围内的“角色和管理员”列表开始操作。

  • 若要向被分派人授予权限以管理特定企业应用的用户和组访问权限,请在 Microsoft Entra ID 中转到该应用,并打开该应用的“角色和管理员”列表。 选择新的自定义角色并完成用户或组分配。 被分派人只能管理特定应用的用户和组访问权限。

  • 若要测试自定义角色分配,请以被分派人身份登录,并打开应用程序的“用户和组”页,验证是否启用了“添加用户”选项 。

    Verify the user permissions

PowerShell

有关更多详细信息,请参阅在 Microsoft Entra ID 中创建和分配自定义角色使用 PowerShell 分配具有资源范围的自定义角色

创建自定义角色

使用以下 PowerShell 脚本创建新角色:

# Basic role information
$description = "Can manage user and group assignments for Applications"
$displayName = "Manage user and group assignments"
$templateId = (New-Guid).Guid

# Set of permissions to grant
$allowedResourceAction = @("microsoft.directory/servicePrincipals/appRoleAssignedTo/update")
$rolePermission = @{'allowedResourceActions'= $allowedResourceAction}
$rolePermissions = $rolePermission

# Create new custom admin role
$customRole = New-MgRoleManagementDirectoryRoleDefinition -Description $description `
   -DisplayName $displayName -RolePermissions $rolePermissions -TemplateId $templateId -IsEnabled

分配自定义角色

使用此 PowerShell 脚本分配角色。

# Get the user and role definition you want to link
$user =  Get-MgUser -Filter "userPrincipalName eq 'chandra@example.com'"
$roleDefinition = Get-MgRoleManagementDirectoryRoleDefinition -Filter "displayName eq 'Manage user and group assignments'"

# Get app registration and construct scope for assignment.
$appRegistration = Get-MgApplication -Filter "displayName eq 'My Filter Photos'"
$directoryScope = '/' + $appRegistration.Id

# Create a scoped role assignment
$roleAssignment = New-MgRoleManagementDirectoryRoleAssignment -DirectoryScopeId $directoryScope `
   -PrincipalId $user.Id -RoleDefinitionId $roleDefinition.Id

Microsoft Graph API

使用 Create unifiedRoleDefinition API 创建自定义角色。 有关更多详细信息,请参阅在 Microsoft Entra ID 中创建和分配自定义角色以及使用 Microsoft Graph API 分配自定义管理员角色

POST https://microsoftgraph.chinacloudapi.cn/v1.0/roleManagement/directory/roleDefinitions

{
    "description": "Can manage user and group assignments for Applications.",
    "displayName": "Manage user and group assignments",
    "isEnabled": true,
    "rolePermissions":
    [
        {
            "allowedResourceActions":
            [
                "microsoft.directory/servicePrincipals/appRoleAssignedTo/update"
            ]
        }
    ],
    "templateId": "<PROVIDE NEW GUID HERE>",
    "version": "1"
}

使用 Microsoft Graph API 分配自定义角色

使用 Create unifiedRoleAssignment API 分配自定义角色。 角色分配会将安全主体 ID(可以是用户或服务主体)、角色定义 ID 和 Microsoft Entra 资源范围合并。 有关角色分配元素的详细信息,请参阅自定义角色概述

POST https://microsoftgraph.chinacloudapi.cn/v1.0/roleManagement/directory/roleAssignments

{
    "@odata.type": "#microsoft.graph.unifiedRoleAssignment",
    "principalId": "<PROVIDE OBJECTID OF USER TO ASSIGN HERE>",
    "roleDefinitionId": "<PROVIDE OBJECTID OF ROLE DEFINITION HERE>",
    "directoryScopeId": "/"
}

后续步骤