在 Microsoft Entra ID 中建立和指派自訂角色

本文說明如何在 Microsoft Entra ID 中建立新的自定義角色。 如需自訂角色的基本知識,請參閱自訂角色概觀。 僅能在目錄層級範圍或應用程式註冊資源範圍指派角色。

您可以在 Microsoft Entra 系統管理中心的 [角色和系統管理員] 頁面中建立自定義角色。

必要條件

  • Microsoft Entra ID P1 或 P2 授權
  • 特殊權限角色管理員或全域管理員
  • 使用 PowerShell 時的 Microsoft.Graph 模組
  • 針對 Microsoft Graph API 使用 Graph 總管時的管理員同意

如需詳細資訊,請參閱 使用PowerShell或 Graph 總管的必要條件。

在 Microsoft Entra 系統管理中心建立角色

建立新的自訂角色以授與可管理應用程式註冊的存取權

提示

本文中的步驟可能會根據您從開始的入口網站稍有不同。

  1. 以至少具特殊許可權的角色 管理員 istrator 身分登入 Microsoft Entra 系統管理中心

  2. 流覽至 [身分>識別角色] 和 [系統管理員>角色] 和 [系統管理員]。

  3. 選取 [ 新增自定義角色]。

    Create or edit roles from the Roles and administrators page

  4. 在 [基本資訊] 索引標籤上提供角色的名稱和描述,然後按一下 [下一步]

    provide a name and description for a custom role on the Basics tab

  5. 在 [權限] 索引標籤上選取應用程式註冊的基本屬性和認證屬性所需的權限。 如需每個許可權的詳細描述,請參閱 Microsoft Entra ID 中的應用程式註冊子類型和許可權。

    1. 首先,在搜尋列中輸入「認證」,然後選取 microsoft.directory/applications/credentials/update 權限。

      Select the permissions for a custom role on the Permissions tab

    2. 接下來,在搜尋列中輸入“basic”,選取 microsoft.directory/applications/basic/update 許可權,然後按 [ 下一步]。

  6. 在 [檢閱 + 建立] 索引標籤上檢閱權限,然後選取 [建立]

您的自訂角色將顯示在要指派的可用角色清單中。

使用PowerShell建立角色

登入

使用 連線-MgGraph 命令來登入您的租使用者。

Connect-MgGraph -Scopes "RoleManagement.ReadWrite.Directory"

建立自定義角色

使用下列 PowerShell 腳本建立新角色:

# Basic role information
$displayName = "Application Support Administrator"
$description = "Can manage basic aspects of application registrations."
$templateId = (New-Guid).Guid
 
# Set of permissions to grant
$allowedResourceAction =
@(
    "microsoft.directory/applications/basic/update",
    "microsoft.directory/applications/credentials/update"
)
$rolePermissions = @(@{AllowedResourceActions= $allowedResourceAction})
 
# Create new custom admin role
$customAdmin = New-MgRoleManagementDirectoryRoleDefinition -RolePermissions $rolePermissions -DisplayName $displayName -IsEnabled -Description $description -TemplateId $templateId

使用 PowerShell 指派自定義角色

使用下列 PowerShell 腳本指派角色:

# Get the user and role definition you want to link
$user = Get-MgUser -Filter "userPrincipalName eq 'cburl@f128.info'"
$roleDefinition = Get-MgRoleManagementDirectoryRoleDefinition -Filter "DisplayName eq 'Application Support Administrator'"

# Get app registration and construct resource scope for assignment.
$appRegistration = Get-MgApplication -Filter "Displayname eq 'POSTMAN'"
$resourceScope = '/' + $appRegistration.objectId

# Create a scoped role assignment
$roleAssignment = New-MgRoleManagementDirectoryRoleAssignment -DirectoryScopeId $resourcescope -RoleDefinitionId $roledefinition.Id -PrincipalId $user.Id

使用 Microsoft Graph API 建立角色

  1. 使用建立 unifiedRoleDefinition API 來建立自定義角色。

    POST https://graph.microsoft.com/v1.0/roleManagement/directory/roleDefinitions
    

    本文

    {
       "description": "Can manage basic aspects of application registrations.",
       "displayName": "Application Support Administrator",
       "isEnabled": true,
       "templateId": "<GUID>",
       "rolePermissions": [
           {
               "allowedResourceActions": [
                   "microsoft.directory/applications/basic/update",
                   "microsoft.directory/applications/credentials/update"
               ]
           }
       ]
    }
    

    注意

    "templateId": "GUID"是根據需求在主體中傳送的選擇性參數。 如果您需要使用一般參數建立多個不同的自定義角色,最好建立範本並定義 templateId 值。 您可以使用 PowerShell Cmdlet (New-Guid).Guid事先產生templateId值。

  2. 使用建立 unifiedRoleAssignment API 來指派自定義角色。

    POST https://graph.microsoft.com/v1.0/roleManagement/directory/roleAssignments
    

    本文

    {
        "principalId":"<GUID OF USER>",
        "roleDefinitionId":"<GUID OF ROLE DEFINITION>",
        "directoryScopeId":"/<GUID OF APPLICATION REGISTRATION>"
    }
    

將範圍設定為資源的自定義角色

如同內建角色,自定義角色預設會在預設的組織範圍內指派,以授與組織中所有應用程式註冊的訪問許可權。 此外,自定義角色和一些相關的內建角色(視 Microsoft Entra 資源類型而定)也可以在單一 Microsoft Entra 資源的範圍內指派。 這可讓您授與使用者更新單一應用程式認證和基本屬性的許可權,而不需要建立第二個自定義角色。

  1. 以至少應用程式開發人員分登入 Microsoft Entra 系統管理中心

  2. 流覽至 [身分>識別應用程式> 應用程式註冊]。

  3. 選取您要授與管理存取權的應用程式註冊。 您可能必須選取 [所有應用程式 ],才能查看 Microsoft Entra 組織中應用程式註冊的完整清單。

    Select the app registration as a resource scope for a role assignment

  4. 在應用程式註冊中,選取 [ 角色和系統管理員]。 如果您尚未建立一個,指示會位於 上述程式中

  5. 選取角色以開啟 [ 指派] 頁面。

  6. 選取 [新增指派 ] 以新增使用者。 使用者只會授與所選應用程式註冊的任何許可權。

下一步