托管标识在 Microsoft Entra ID 中提供了一个自动托管标识,供应用程序在连接到支持 Microsoft Entra 身份验证的资源时使用。 可以直接向托管标识的服务主体授予 API 权限,而无需在代码或配置文件中存储凭据。
本文介绍如何使用 Microsoft Entra PowerShell 为系统分配的托管标识和用户分配的托管标识授予和撤销Microsoft 图形 API权限。
先决条件
- 一个 Microsoft Entra 用户帐户。 如果还没有帐户,可以免费创建一个帐户。
- 安装 Microsoft Entra PowerShell 模块。
- 启用系统分配的或用户分配的托管标识的 Azure 资源。
- 若要向托管标识授予 API 权限,需要以下角色之一:
向托管标识授予 API 权限
按照以下步骤为托管标识授予 Microsoft 图形 API 权限。
连接到Microsoft Entra
要向托管标识授予 API 权限,请使用
Application.ReadWrite.All和AppRoleAssignment.ReadWrite.All范围连接:Connect-Entra -Scopes "Application.ReadWrite.All", "AppRoleAssignment.ReadWrite.All"注意
该
AppRoleAssignment.ReadWrite.All权限允许应用或服务管理组织中任何应用、用户或组的权限授予和提升权限。 仅向受信任的管理员授予此权限。识别托管标识服务主体
在 Microsoft Entra ID 中查找与您的托管标识对应的服务主体。
对于系统分配的托管标识,请使用Azure资源名称:
$managedIdentityName = "MyAzureVM" $managedIdentitySP = Get-EntraServicePrincipal -Filter "displayName eq '$managedIdentityName' and servicePrincipalType eq 'ManagedIdentity'" if (-not $managedIdentitySP) { Write-Error "Managed identity service principal '$managedIdentityName' not found." -ErrorAction Stop } Write-Host "Found managed identity service principal:" Write-Host "Display Name: $($managedIdentitySP.DisplayName)" Write-Host "Object ID: $($managedIdentitySP.Id)"注释
如果筛选器返回多个结果(例如,当多个托管标识共享相同的显示名称时),请使用对象 ID 标识正确的服务主体:
Get-EntraServicePrincipal -ServicePrincipalId '<object-id>'对于用户分配的托管标识,请使用托管标识名称:
$userAssignedMIName = "MyUserAssignedMI" $managedIdentitySP = Get-EntraServicePrincipal -Filter "displayName eq '$userAssignedMIName' and servicePrincipalType eq 'ManagedIdentity'" if (-not $managedIdentitySP) { Write-Error "Managed identity service principal '$userAssignedMIName' not found." -ErrorAction Stop }获取 Microsoft Graph 服务主体
获取 Microsoft Graph 服务主体:
$graphServicePrincipal = Get-EntraServicePrincipal -Filter "appId eq '00000003-0000-0000-c000-000000000000'"标识所需的 API 权限
查找你的托管标识所需的特定 Microsoft Graph 权限:
$appRole = $graphServicePrincipal.AppRoles | Where-Object { $_.Value -eq "User.Read.All" }向托管标识授予 API 权限
向你的托管标识授予 API 权限:
$params = @{ ServicePrincipalId = $managedIdentitySP.Id PrincipalId = $managedIdentitySP.Id ResourceId = $graphServicePrincipal.Id AppRoleId = $appRole.Id } $appRoleAssignment = New-EntraServicePrincipalAppRoleAssignment @params验证授予的权限
确认权限已成功授予:
$assignments = Get-EntraServicePrincipalAppRoleAssignment -ServicePrincipalId $managedIdentitySP.Id Write-Host "Current app role assignments for $($managedIdentitySP.DisplayName):" foreach ($assignment in $assignments) { $resource = Get-EntraServicePrincipal -ServicePrincipalId $assignment.ResourceId $assignedRole = $resource.AppRoles | Where-Object { $_.Id -eq $assignment.AppRoleId } Write-Host "- Resource: $($resource.DisplayName)" Write-Host " Permission: $($assignedRole.Value)" }
从托管标识撤销 API 权限
按照以下步骤从托管标识中撤销 Microsoft 图形 API 权限。
标识要删除的权限
查找要撤销的特定应用角色分配:
$assignmentToRemove = Get-EntraServicePrincipalAppRoleAssignment -ServicePrincipalId $managedIdentitySP.Id | Where-Object { $_.AppRoleId -eq $appRole.Id -and $_.ResourceId -eq $graphServicePrincipal.Id }删除应用角色分配
使用应用 ID 删除应用角色分配:
if ($assignmentToRemove) { Remove-EntraServicePrincipalAppRoleAssignment -ServicePrincipalId $managedIdentitySP.Id -AppRoleAssignmentId $assignmentToRemove.Id Write-Host "Successfully removed permission: $($appRole.Value)" }验证权限是否已删除
确认已吊销的权限没有剩余分配:
$remainingAssignments = Get-EntraServicePrincipalAppRoleAssignment -ServicePrincipalId $managedIdentitySP.Id if ($remainingAssignments) { Write-Host "Remaining permissions for $($managedIdentitySP.DisplayName):" foreach ($assignment in $remainingAssignments) { $resource = Get-EntraServicePrincipal -ServicePrincipalId $assignment.ResourceId $assignedRole = $resource.AppRoles | Where-Object { $_.Id -eq $assignment.AppRoleId } Write-Host "- $($assignedRole.Value) on $($resource.DisplayName)" } } else { Write-Host "No app role assignments remain for $($managedIdentitySP.DisplayName)." }