管理应用程序的用户和组分配

本文介绍如何使用 PowerShell 将用户和组分配到 Microsoft Entra ID 中的企业应用程序。 将用户分配到某个应用程序时,该应用程序会显示在该用户的我的应用门户中以方便访问。 如果应用程序公开应用角色,则你还可以将特定的应用角色分配给用户。

将某个组分配给应用程序时,只有属于该组的用户才拥有访问权限。 该分配不会级联到嵌套组。

基于组的分配需要使用 Microsoft Entra ID P1 或 P2 版本。 仅安全组和将设置 SecurityEnabled 设置为 True 的 Microsoft 365 组支持基于组的分配。 目前不支持嵌套组成员身份。 有关本文中讨论的功能的其他许可要求,请参阅 Microsoft Entra ID 定价页

为了提高控制度,可将某些类型的企业应用程序配置为需要进行用户分配。 有关要求为应用分配用户的更多信息,请参阅管理对应用程序的访问

先决条件

若要将用户分配给企业应用程序,你需要:

  • 具有活动订阅的 Microsoft Entra 帐户。 如果还没有帐户,可以免费创建一个帐户
  • 以下角色之一:全局管理员、云应用程序管理员、应用程序管理员或服务主体的所有者。
  • 面向基于组的分配的 Microsoft Entra ID P1 或 P2。 有关本文中讨论的功能的其他许可要求,请参阅 Microsoft Entra ID 定价页

提示

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

使用 Microsoft Entra 管理中心将用户和组分配给 Microsoft Entra ID 的应用程序

若要将用户或组帐户分配给企业应用程序,请执行以下操作:

  1. 至少以云应用程序管理员身份登录到 Microsoft Entra 管理中心

  2. 浏览到“标识”>“应用程序”>“企业应用程序”>“所有应用程序”。

  3. 在“搜索”框中输入现有应用程序的名称,然后从搜索结果中选择该应用程序。

  4. 依次选择“用户和组”和“添加用户/组”。

    Assign user account to an application in your Microsoft Entra tenant.

  5. 在“添加分配”窗格中,选择“用户和组”下的“未选择任何内容”。

  6. 搜索并选择要分配给应用程序的用户或组。 例如,contosouser1@contoso.comcontosoteam1@contoso.com

  7. 选择“选择” 。

  8. 在“选择角色”下,选择想要分配给该用户或组的角色。 如果尚未定义任何角色,则默认角色为“默认访问”

  9. 在“添加分配”窗格中,选择“分配”以将用户或组分配给应用程序。

从应用程序取消分配用户和组

  1. 按照向应用程序分配用户和组部分中的步骤导航到“用户和组”窗格。
  2. 搜索并选择要从应用程序取消分配的用户或组。
  3. 选择“删除”以从应用程序取消分配用户或组。

使用 Azure AD PowerShell 将用户和组分配给应用程序

  1. 以提升的权限打开 Windows PowerShell 命令提示符。

  2. 运行 Connect-AzureAD 并至少以云应用程序管理员的身份登录。

  3. 使用以下脚本将用户和角色分配到应用程序:

    # Assign the values to the variables
    $username = "<Your user's UPN>"
    $app_name = "<Your App's display name>"
    $app_role_name = "<App role display name>"
    
    # Get the user to assign, and the service principal for the app to assign to
    $user = Get-AzureADUser -ObjectId "$username"
    $sp = Get-AzureADServicePrincipal -Filter "displayName eq '$app_name'"
    $appRole = $sp.AppRoles | Where-Object { $_.DisplayName -eq $app_role_name }
    
    # Assign the user to the app role
    New-AzureADUserAppRoleAssignment -ObjectId $user.ObjectId -PrincipalId $user.ObjectId -ResourceId $sp.ObjectId -Id $appRole.Id
    

若要将组分配到企业应用,必须将 Get-AzureADUser 替换为 Get-AzureADGroup,并将 New-AzureADUserAppRoleAssignment 替换为 New-AzureADGroupAppRoleAssignment

有关如何将组分配到应用程序角色的详细信息,请参阅 New-AzureADGroupAppRoleAssignment 的文档。

示例

此示例使用 PowerShell 将用户 Britta Simon 分配到 Microsoft Workplace Analytics 应用程序。

  1. 在 PowerShell 中,将相应的值分配到变量 $username、$app_name 和 $app_role_name。

    # Assign the values to the variables
    $username = "britta.simon@contoso.com"
    $app_name = "Workplace Analytics"
    
  2. 在此示例中,我们并不确切地知道要将哪个应用程序角色名称分配给 Britta Simon。 运行以下命令,使用用户 UPN 和服务主体显示名称获取用户 ($user) 和服务主体 ($sp)。

    # Get the user to assign, and the service principal for the app to assign to
    $user = Get-AzureADUser -ObjectId "$username"
    $sp = Get-AzureADServicePrincipal -Filter "displayName eq '$app_name'"
    
  3. 运行命令 $sp.AppRoles,显示可用于 Workplace Analytics 应用程序的角色。 在此示例中,我们要为 Britta Simon 分配“分析员”(访问权限受限)角色。 Shows the roles available to a user using Workplace Analytics Role

  4. 将角色名称分配到 $app_role_name 变量。

    # Assign the values to the variables
    $app_role_name = "Analyst (Limited access)"
    $appRole = $sp.AppRoles | Where-Object { $_.DisplayName -eq $app_role_name }
    
  5. 运行以下命令,将用户分配到应用角色:

    # Assign the user to the app role
    New-AzureADUserAppRoleAssignment -ObjectId $user.ObjectId -PrincipalId $user.ObjectId -ResourceId $sp.ObjectId -Id $appRole.Id
    

使用 Azure AD PowerShell 从应用程序中取消分配用户和组

  1. 以提升的权限打开 Windows PowerShell 命令提示符。

  2. 运行 Connect-AzureAD 并至少以云应用程序管理员的身份登录。

  3. 使用以下脚本将用户和角色从应用程序移除。

    # Store the proper parameters
    $user = get-azureaduser -ObjectId <objectId>
    $spo = Get-AzureADServicePrincipal -ObjectId <objectId>
    
    #Get the ID of role assignment 
    $assignments = Get-AzureADServiceAppRoleAssignment -ObjectId $spo.ObjectId | Where {$_.PrincipalDisplayName -eq $user.DisplayName}
    
    #if you run the following, it will show you what is assigned what
    $assignments | Select *
    
    #To remove the App role assignment run the following command.
    Remove-AzureADServiceAppRoleAssignment -ObjectId $spo.ObjectId -AppRoleAssignmentId $assignments[assignment number].ObjectId
    

使用 Azure AD PowerShell 删除分配给应用程序的所有用户

使用以下脚本移除分配给应用程序的所有用户和组。

#Retrieve the service principal object ID.
$app_name = "<Your App's display name>"
$sp = Get-AzureADServicePrincipal -Filter "displayName eq '$app_name'"
$sp.ObjectId

# Get Service Principal using objectId
$sp = Get-AzureADServicePrincipal -ObjectId "<ServicePrincipal objectID>"

# Get Azure AD App role assignments using objectId of the Service Principal
$assignments = Get-AzureADServiceAppRoleAssignment -ObjectId $sp.ObjectId -All $true

# Remove all users and groups assigned to the application
$assignments | ForEach-Object {
    if ($_.PrincipalType -eq "User") {
        Remove-AzureADUserAppRoleAssignment -ObjectId $_.PrincipalId -AppRoleAssignmentId $_.ObjectId
    } elseif ($_.PrincipalType -eq "Group") {
        Remove-AzureADGroupAppRoleAssignment -ObjectId $_.PrincipalId -AppRoleAssignmentId $_.ObjectId
    }
}

使用 Microsoft Graph PowerShell 将用户和组分配给应用程序

  1. 以提升的权限打开 Windows PowerShell 命令提示符。
  2. 运行 Connect-MgGraph -Scopes "Application.ReadWrite.All", "Directory.ReadWrite.All", "AppRoleAssignment.ReadWrite.All" 并至少以云应用程序管理员的身份登录。
  3. 使用以下脚本将用户和角色分配到应用程序:

# Assign the values to the variables

$userId = "<Your user's ID>"
$app_name = "<Your App's display name>"
$app_role_name = "<App role display name>"
$sp = Get-MgServicePrincipal -Filter "displayName eq '$app_name'"

# Get the user to assign, and the service principal for the app to assign to

$params = @{
    "PrincipalId" =$userId
    "ResourceId" =$sp.Id
    "AppRoleId" =($sp.AppRoles | Where-Object { $_.DisplayName -eq $app_role_name }).Id
    }

# Assign the user to the app role

New-MgUserAppRoleAssignment -UserId $userId -BodyParameter $params |
    Format-List Id, AppRoleId, CreationTime, PrincipalDisplayName,
    PrincipalId, PrincipalType, ResourceDisplayName, ResourceId

使用 Microsoft Graph PowerShell 从应用程序中取消分配用户和组

  1. 以提升的权限打开 Windows PowerShell 命令提示符。
  2. 运行 Connect-MgGraph -Scopes "Application.ReadWrite.All", "Directory.ReadWrite.All", "AppRoleAssignment.ReadWrite.All" 并至少以云应用程序管理员的身份登录。 使用以下脚本将用户和角色从应用程序移除。

# Get the user and the service principal

$user = Get-MgUser -UserId <userid>
$spo = Get-MgServicePrincipal -ServicePrincipalId <ServicePrincipalId>

# Get the Id of the role assignment

$assignments = Get-MgServicePrincipalAppRoleAssignedTo -ServicePrincipalId $spo.Id | Where {$_.PrincipalDisplayName -eq $user.DisplayName}

# if you run the following, it will show you the list of users assigned to the application

$assignments | Select *

# To remove the App role assignment run the following command.

Remove-MgServicePrincipalAppRoleAssignedTo -AppRoleAssignmentId  '<AppRoleAssignment-id>' -ServicePrincipalId $spo.Id

使用 Microsoft Graph PowerShell 删除分配给应用程序的所有用户和组

使用以下脚本移除分配给应用程序的所有用户和组。

$assignments | ForEach-Object {
    if ($_.PrincipalType -in ("user", "Group")) {
        Remove-MgServicePrincipalAppRoleAssignedTo -ServicePrincipalId $Sp.Id -AppRoleAssignmentId $_.Id  }
}

使用 Microsoft Graph API 将用户和组分配给应用程序

  1. 若要向应用程序分配用户和组,请至少以云应用程序管理员身份登录到 Graph 资源管理器

    你需要同意以下权限:

    Application.ReadWrite.AllDirectory.ReadWrite.AllAppRoleAssignment.ReadWrite.All

    若要授予应用角色分配,需要三个标识符:

    • principalId:要向其分配应用角色的用户或组的 ID。
    • resourceId:定义应用角色的资源 servicePrincipal 的 ID。
    • appRoleId:要分配给用户或组的 appRole(在资源服务主体上定义)的 ID。
  2. 获取企业应用程序。 按 DisplayName 筛选。

    GET https://graph.microsoft.com/v1.0/servicePrincipals?$filter=displayName eq '{appDisplayName}'
    

    记录响应正文中的以下值:

    • 企业应用程序的对象 ID
    • 分配给用户的 appRoleId。 如果应用程序不公开任何角色,则将为用户分配默认访问角色。
  3. 通过按用户的主体名称进行筛选来获取用户。 记录用户的对象 ID。

    GET https://graph.microsoft.com/v1.0/users/{userPrincipalName}
    
  4. 请将该用户分配到应用程序。

    POST https://graph.microsoft.com/v1.0/servicePrincipals/{resource-servicePrincipal-id}/appRoleAssignedTo
    
    {
    "principalId": "33ad69f9-da99-4bed-acd0-3f24235cb296",
    "resourceId": "9028d19c-26a9-4809-8e3f-20ff73e2d75e",
    "appRoleId": "ef7437e6-4f94-4a0a-a110-a439eb2aa8f7"
    }
    

    在此示例中,resource-servicePrincipal-id 和 resourceId 都表示企业应用程序。

使用 Microsoft Graph API 从应用程序取消分配用户和组

若要从应用程序取消分配用户和组,请运行以下查询。

  1. 获取企业应用程序。 按 displayName 筛选。

    GET https://graph.microsoft.com/v1.0/servicePrincipals?$filter=displayName eq '{appDisplayName}'
    
  2. 获取应用程序的 appRoleAssignments 列表。

    GET https://graph.microsoft.com/v1.0/servicePrincipals/{id}/appRoleAssignedTo
    
  3. 通过指定 appRoleAssignment ID 移除 appRoleAssignments。

    DELETE https://graph.microsoft.com/v1.0/servicePrincipals/{resource-servicePrincipal-id}/appRoleAssignedTo/{appRoleAssignment-id}
    

后续步骤