本文介绍如何在 Microsoft Entra ID 中将用户和组分配到企业应用程序。 将用户分配到某个应用程序时,该应用程序会显示在该用户的我的应用门户中以方便访问。 如果应用程序公开应用角色,则你还可以将特定的应用角色分配给用户。
将某个组分配给应用程序时,只有属于该组的用户才拥有访问权限。 该分配不会级联到嵌套组。
基于组的分配需要使用 Microsoft Entra ID P1 或 P2 版本。 安全组、Microsoft 365 组和通讯组支持基于组的分配,其 SecurityEnabled 设置将仅设置为 True。 目前不支持嵌套组成员身份。 有关本文中讨论的功能的其他许可要求,请参阅 Microsoft Entra ID 定价页。
# 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
示例
此示例使用 PowerShell 将用户 Britta Simon 分配到 Microsoft Workplace Analytics 应用程序。
# 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 删除分配给应用程序的所有用户
以提升的权限打开 Windows 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
}
}
# Get the service principal for the app
$sp = Get-MgServicePrincipal -Filter "displayName eq '$app_name'"
运行以下命令以查找服务主体公开的应用角色。
# Get the app roles exposed by the service principal
$appRoles = $sp.AppRoles
# Display the app roles
$appRoles | ForEach-Object {
Write-Output "AppRole: $($_.DisplayName) - ID: $($_.Id)"
}
备注
默认的 AppRole ID 为 00000000-0000-0000-0000-000000000000。 如果未为服务主体定义特定的 AppRole,则会分配此角色。
将角色名称分配到 $app_role_name 变量。 在此示例中,我们要为 Britta Simon 分配“分析员”(访问权限受限)角色。
# Assign the values to the variables
$app_role_name = "Analyst (Limited access)"
$appRoleId = ($sp.AppRoles | Where-Object { $_.DisplayName -eq $app_role_name }).Id
准备参数并运行以下命令,将用户分配到应用角色。
# Prepare parameters for the role assignment
$params = @{
"PrincipalId" = $userId
"ResourceId" = $sp.Id
"AppRoleId" = $appRoleId
}
# Assign the user to the app role
New-MgUserAppRoleAssignment -UserId $userId -BodyParameter $params |
Format-List Id, AppRoleId, CreationTime, PrincipalDisplayName,
PrincipalId, PrincipalType, ResourceDisplayName, ResourceId