在不同的范围分配 Microsoft Entra 角色

在 Microsoft Entra ID 中,通常会分配 Microsoft Entra 角色,以便它们应用于整个租户。 但是,还可以为不同的资源(例如管理单元或应用程序注册)分配 Microsoft Entra 角色。 例如,可以分配支持管理员角色,使之仅应用于特定的管理单元,而不是整个租户。 角色分配适用的资源也称为范围。 本文介绍如何在租户范围、管理单元范围和应用程序注册范围分配 Microsoft Entra 角色。 有关范围的详细信息,请参阅 Microsoft Entra ID 中基于角色的访问控制 (RBAC) 概述

先决条件

  • 特权角色管理员或全局管理员。
  • 使用 PowerShell 时安装的 Microsoft Graph PowerShell SDK。
  • 将 Graph 浏览器用于 Microsoft Graph API 时需要管理员同意。

有关详细信息,请参阅使用 PowerShell 或 Graph 浏览器的先决条件

分配以租户为作用域的角色

本部分介绍如何在租户作用域分配角色。

Microsoft Entra 管理中心

提示

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

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

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

    Roles and administrators page in Microsoft Entra ID.

  3. 选择一个角色,查看其分配情况。 为帮助查找所需角色,请使用“添加筛选器”来筛选角色。

  4. 选择“添加分配”,然后选择要分配给此角色的用户。

    Add assignments pane for selected role.

  5. 选择“添加”以分配角色。

PowerShell

执行以下步骤以使用 PowerShell 分配 Microsoft Entra 角色。

  1. 打开 PowerShell 窗口。 如有必要,使用 Install-Module 安装 Microsoft Graph PowerShell。 有关详细信息,请参阅使用 PowerShell 或 Graph 浏览器的先决条件

    Install-Module Microsoft.Graph -Scope CurrentUser
    
  2. 在 PowerShell 窗口中,使用 Connect-MgGraph 登录到你的租户。

    Connect-MgGraph -Scopes "RoleManagement.Read.Directory","User.Read.All","RoleManagement.ReadWrite.Directory"
    
  3. 使用 Get-MgUser 获取用户。

    $user = Get-MgUser -Filter "userPrincipalName eq 'alice@contoso.com'"
    
  4. 使用 Get-MgRoleManagementDirectoryRoleDefinition 获取要分配的角色。

    $roleDefinition = Get-MgRoleManagementDirectoryRoleDefinition -Filter "displayName eq 'Billing Administrator'"
    
  5. 将租户设置为角色分配的作用域。

    $directoryScope = '/'
    
  6. 使用 New-MgRoleManagementDirectoryRoleAssignment 分配角色。

    $roleAssignment = New-MgRoleManagementDirectoryRoleAssignment `
       -DirectoryScopeId $directoryScope -PrincipalId $user.Id `
       -RoleDefinitionId $roleDefinition.Id
    

Microsoft Graph API

按照这些说明使用 Graph 浏览器中的 Microsoft Graph API 分配角色。

  1. 登录到 Graph 浏览器

  2. 使用 List users API 获取用户。

    GET https://graph.microsoft.com/v1.0/users?$filter=userPrincipalName eq 'alice@contoso.com'
    
  3. 使用 List unifiedRoleDefinitions API 获取想要分配的角色。

    GET https://graph.microsoft.com/v1.0/rolemanagement/directory/roleDefinitions?$filter=displayName eq 'Billing Administrator'
    
  4. 使用 Create unifiedRoleAssignment API 分配角色。

    POST https://graph.microsoft.com/v1.0/roleManagement/directory/roleAssignments
    {
        "@odata.type": "#microsoft.graph.unifiedRoleAssignment",
        "principalId": "<provide objectId of the user obtained above>",
        "roleDefinitionId": "<provide templateId of the role obtained above>",
        "directoryScopeId": "/"
    }
    

分配以管理单元为作用域的角色

本部分介绍如何在管理单元范围分配角色。

Microsoft Entra 管理中心

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

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

  3. 选择管理单元。

    Administrative Units in Microsoft Entra ID.

  4. 从左侧导航菜单中选择“角色和管理员”,查看可用于通过管理单元分配的所有角色的列表。

    Roles and administrators menu under administrative Units in Microsoft Entra ID.

  5. 选择所需的角色。

  6. 选择“添加分配”,然后选择要向其分配此角色的用户或组。

  7. 选择“添加”分配以管理单元为作用域的角色。

注意

你不会在此处看到 Microsoft Entra 内置或自定义角色的完整列表。 这是正常情况。 我们将显示具有与管理单元内支持的对象相关的权限的角色。 若要查看管理单元内支持的对象列表,请参阅 Microsoft Entra ID 中的管理单元

PowerShell

执行以下步骤,使用 PowerShell 在管理单元作用域分配 Microsoft Entra 角色。

  1. 打开 PowerShell 窗口。 如有必要,使用 Install-Module 安装 Microsoft Graph PowerShell。 有关详细信息,请参阅使用 PowerShell 或 Graph 浏览器的先决条件

    Install-Module Microsoft.Graph -Scope CurrentUser
    
  2. 在 PowerShell 窗口中,使用 Connect-MgGraph 登录到你的租户。

    Connect-MgGraph -Scopes "Directory.Read.All","RoleManagement.Read.Directory","User.Read.All","RoleManagement.ReadWrite.Directory"
    
  3. 使用 Get-MgUser 获取用户。

    $user = Get-MgUser -Filter "userPrincipalName eq 'alice@contoso.com'"
    
  4. 使用 Get-MgRoleManagementDirectoryRoleDefinition 获取要分配的角色。

    $roleDefinition = Get-MgRoleManagementDirectoryRoleDefinition `
       -Filter "displayName eq 'User Administrator'"
    
  5. 使用 Get-MgDirectoryAdministrativeUnit 获取要将角色分配的作用域限定到的管理单元。

    $adminUnit = Get-MgDirectoryAdministrativeUnit -Filter "displayName eq 'Seattle Admin Unit'"
    $directoryScope = '/administrativeUnits/' + $adminUnit.Id
    
  6. 使用 New-MgRoleManagementDirectoryRoleAssignment 分配角色。

    $roleAssignment = New-MgRoleManagementDirectoryRoleAssignment `
       -DirectoryScopeId $directoryScope -PrincipalId $user.Id `
       -RoleDefinitionId $roleDefinition.Id
    

Microsoft Graph API

按照这些说明使用 Graph 浏览器中的 Microsoft Graph API 在管理单元作用域分配角色。

  1. 登录到 Graph 浏览器

  2. 使用 List users API 获取用户。

    GET https://graph.microsoft.com/v1.0/users?$filter=userPrincipalName eq 'alice@contoso.com'
    
  3. 使用 List unifiedRoleDefinitions API 获取想要分配的角色。

    GET https://graph.microsoft.com/v1.0/rolemanagement/directory/roleDefinitions?$filter=displayName eq 'User Administrator'
    
  4. 使用列出 administrativeUnits API 获取要将角色分配的作用域限定到的管理单元。

    GET https://graph.microsoft.com/v1.0/directory/administrativeUnits?$filter=displayName eq 'Seattle Admin Unit'
    
  5. 使用 Create unifiedRoleAssignment API 分配角色。

    POST https://graph.microsoft.com/v1.0/roleManagement/directory/roleAssignments
    {
        "@odata.type": "#microsoft.graph.unifiedRoleAssignment",
        "principalId": "<provide objectId of the user obtained above>",
        "roleDefinitionId": "<provide templateId of the role obtained above>",
        "directoryScopeId": "/administrativeUnits/<provide objectId of the admin unit obtained above>"
    }
    

注意

此处 directoryScopeId 被指定为 /administrativeUnits/foo,而不是 /foo。 这是默认设置。 作用域 /administrativeUnits/foo 表示主体可以管理管理单元的成员(基于分配给她的角色),而不是管理单元本身。 /foo 作用域表示主体可以管理该 Microsoft Entra 对象本身。 在后续部分中,你将看到作用域为 /foo,因为以应用注册为作用域的角色授予管理对象本身的特权。

分配以应用注册为作用域的角色

本部分介绍如何在应用程序注册范围分配角色。

Microsoft Entra 管理中心

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

  2. 浏览到“标识”>“应用程序”>“应用注册”。

  3. 选择应用程序。 可以使用“搜索”框查找所需的应用。

    App registrations in Microsoft Entra ID.

  4. 从左侧导航菜单中选择“角色和管理员”,查看可用于通过应用注册分配的所有角色的列表。

    Roles for an app registrations in Microsoft Entra ID.

  5. 选择所需的角色。

  6. 选择“添加分配”,然后选择要向其分配此角色的用户或组。

    Add role assignment scoped to an app registrations in Microsoft Entra ID.

  7. 选择“添加”分配以应用注册为作用域的角色。

    Successfully added role assignment scoped to an app registrations in Microsoft Entra ID.

    Role assigned to the user scoped to an app registrations in Microsoft Entra ID.

注意

你不会在此处看到 Microsoft Entra 内置或自定义角色的完整列表。 这是正常情况。 我们将显示仅具有与管理应用注册相关的权限的角色。

PowerShell

执行以下步骤,使用 PowerShell 在应用程序作用域分配 Microsoft Entra 角色。

  1. 打开 PowerShell 窗口。 如有必要,使用 Install-Module 安装 Microsoft Graph PowerShell。 有关详细信息,请参阅使用 PowerShell 或 Graph 浏览器的先决条件

    Install-Module Microsoft.Graph -Scope CurrentUser
    
  2. 在 PowerShell 窗口中,使用 Connect-MgGraph 登录到你的租户。

    Connect-MgGraph -Scopes "Application.Read.All","RoleManagement.Read.Directory","User.Read.All","RoleManagement.ReadWrite.Directory"
    
  3. 使用 Get-MgUser 获取用户。

    $user = Get-MgUser -Filter "userPrincipalName eq 'alice@contoso.com'"
    
  4. 使用 Get-MgRoleManagementDirectoryRoleDefinition 获取要分配的角色。

    $roleDefinition = Get-MgRoleManagementDirectoryRoleDefinition `
       -Filter "displayName eq 'Application Administrator'"
    
  5. 使用 Get-MgApplication 获取要将角色分配的作用域限定到的应用注册。

    $appRegistration = Get-MgApplication -Filter "displayName eq 'f/128 Filter Photos'"
    $directoryScope = '/' + $appRegistration.Id
    
  6. 使用 New-MgRoleManagementDirectoryRoleAssignment 分配角色。

    $roleAssignment = New-MgRoleManagementDirectoryRoleAssignment `
       -DirectoryScopeId $directoryScope -PrincipalId $user.Id `
       -RoleDefinitionId $roleDefinition.Id 
    

Microsoft Graph API

按照这些说明使用 Graph 浏览器中的 Microsoft Graph API 在应用程序作用域分配角色。

  1. 登录到 Graph 浏览器

  2. 使用 List users API 获取用户。

    GET https://graph.microsoft.com/v1.0/users?$filter=userPrincipalName eq 'alice@contoso.com'
    
  3. 使用 List unifiedRoleDefinitions API 获取想要分配的角色。

    GET https://graph.microsoft.com/v1.0/rolemanagement/directory/roleDefinitions?$filter=displayName eq 'Application Administrator'
    
  4. 使用列出应用程序 API 获取要将角色分配的作用域限定到的管理单元。

    GET https://graph.microsoft.com/v1.0/applications?$filter=displayName eq 'f/128 Filter Photos'
    
  5. 使用 Create unifiedRoleAssignment API 分配角色。

    POST https://graph.microsoft.com/v1.0/roleManagement/directory/roleAssignments
    
    {
        "@odata.type": "#microsoft.graph.unifiedRoleAssignment",
        "principalId": "<provide objectId of the user obtained above>",
        "roleDefinitionId": "<provide templateId of the role obtained above>",
        "directoryScopeId": "/<provide objectId of the app registration obtained above>"
    }
    

注意

此处的 directoryScopeId 被指定为 /foo,与上面的部分不同。 这是默认设置。 /foo 作用域表示主体可以管理该 Microsoft Entra 对象。 作用域 /administrativeUnits/foo 表示主体可以管理管理单元的成员(基于分配给她的角色),而不是管理单元本身。

后续步骤