管理应用程序的用户分配

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

先决条件

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

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

提示

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

使用 Microsoft Entra 管理中心将用户分配到应用程序

要向企业应用程序分配用户户,请:

  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 -AzureEnvironmentName AzureChinaCloud 并至少以云应用程序管理员的身份登录。

  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
    

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

示例

此示例使用 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 -AzureEnvironmentName AzureChinaCloud 并至少以云应用程序管理员的身份登录。

  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 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 -Environment China -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 -Environment China -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  }
}

后续步骤