删除 Azure 角色分配

Azure 基于角色的访问控制 (Azure RBAC) 是用于管理 Azure 资源访问权限的授权系统。 若要从 Azure 资源删除访问权限,请删除角色分配。 本文介绍如何使用 Azure 门户、Azure PowerShell、Azure CLI 和 REST API 来删除角色分配。

先决条件

若要删除角色分配,你必须具有:

对于 REST API,必须使用以下版本:

  • 2015-07-01 或更高版本

有关详细信息,请参阅 Azure RBAC REST API 的 API 版本

Azure 门户

  1. 在要删除访问权限的范围(例如管理组、订阅、资源组或资源)内打开“访问控制(IAM)”。

  2. 单击“角色分配”选项卡以查看在此范围内的所有角色分配。

  3. 在角色分配列表中,在需删除其角色分配的安全主体旁边添加复选标记。

    Role assignment selected to be removed

  4. 单击“移除”。

    Remove role assignment message

  5. 在显示的“删除角色分配”消息中,单击“是”。

    此时会显示一条消息,指出无法删除继承的角色分配,而你正在尝试删除子范围的角色分配。 应在角色的分配范围打开“访问控制(IAM)”,然后重试。 在正确的范围打开“访问控制(IAM)”的快捷方法是查看“范围”列,然后单击“(继承)”旁边的链接。

    Remove role assignment message for inherited role assignments

Azure PowerShell

在 Azure PowerShell 中,通过使用 Remove-AzRoleAssignment 来删除角色分配。

以下示例在 pharma-sales 资源组上从 patlong@contoso.com 用户删除虚拟机参与者角色分配:

PS C:\> Remove-AzRoleAssignment -SignInName patlong@contoso.com `
-RoleDefinitionName "Virtual Machine Contributor" `
-ResourceGroupName pharma-sales

在订阅范围内将读者角色从 ID 为 22222222-2222-2222-2222-222222222222 的“Ann Mack 团队”组中删除。

PS C:\> Remove-AzRoleAssignment -ObjectId 22222222-2222-2222-2222-222222222222 `
-RoleDefinitionName "Reader" `
-Scope "/subscriptions/00000000-0000-0000-0000-000000000000"

在管理组范围内将账单读者角色从 alain@example.com 用户中删除。

PS C:\> Remove-AzRoleAssignment -SignInName alain@example.com `
-RoleDefinitionName "Billing Reader" `
-Scope "/providers/Microsoft.Management/managementGroups/marketing-group"

如果收到错误消息:“提供的信息未映射到角色分配”,请确保还指定了 -Scope-ResourceGroupName 参数。 有关详细信息,请参阅 Azure RBAC 疑难解答

Azure CLI

在 Azure CLI 中,通过使用 az role assignment delete 来删除角色分配。

以下示例在 pharma-sales 资源组上从 patlong@contoso.com 用户删除虚拟机参与者角色分配:

az role assignment delete --assignee "patlong@contoso.com" \
--role "Virtual Machine Contributor" \
--resource-group "pharma-sales"

在订阅范围内将读者角色从 ID 为 22222222-2222-2222-2222-222222222222 的“Ann Mack 团队”组中删除。

az role assignment delete --assignee "22222222-2222-2222-2222-222222222222" \
--role "Reader" \
--scope "/subscriptions/00000000-0000-0000-0000-000000000000"

在管理组范围内将账单读者角色从 alain@example.com 用户中删除。

az role assignment delete --assignee "alain@example.com" \
--role "Billing Reader" \
--scope "/providers/Microsoft.Management/managementGroups/marketing-group"

REST API

在 REST API 中,通过使用角色分配 - 删除来删除角色分配。

  1. 获取角色分配标识符 (GUID)。 首次创建角色分配时将返回此标识符,也可以通过列出角色分配来获取它。

  2. 从下面的请求开始:

    DELETE https://management.chinacloudapi.cn/{scope}/providers/Microsoft.Authorization/roleAssignments/{roleAssignmentId}?api-version=2022-04-01
    
  3. 在 URI 内,将“{scope}”替换为删除角色分配的范围。

    作用域 类型
    providers/Microsoft.Management/managementGroups/{groupId1} 管理组
    subscriptions/{subscriptionId1} 订阅
    subscriptions/{subscriptionId1}/resourceGroups/myresourcegroup1 资源组
    subscriptions/{subscriptionId1}/resourceGroups/myresourcegroup1/providers/microsoft.web/sites/mysite1 资源
  4. 将“{roleAssignmentId}”替换为角色分配的 GUID 标识符。

以下请求在订阅范围内删除指定的角色分配:

DELETE https://management.chinacloudapi.cn/subscriptions/{subscriptionId1}/providers/microsoft.authorization/roleassignments/{roleAssignmentId1}?api-version=2022-04-01

下面显示了输出示例:

{
    "properties": {
        "roleDefinitionId": "/subscriptions/{subscriptionId1}/providers/Microsoft.Authorization/roleDefinitions/a795c7a0-d4a2-40c1-ae25-d81f01202912",
        "principalId": "{objectId1}",
        "principalType": "User",
        "scope": "/subscriptions/{subscriptionId1}",
        "condition": null,
        "conditionVersion": null,
        "createdOn": "2022-05-06T23:55:24.5379478Z",
        "updatedOn": "2022-05-06T23:55:24.5379478Z",
        "createdBy": "{createdByObjectId1}",
        "updatedBy": "{updatedByObjectId1}",
        "delegatedManagedIdentityResourceId": null,
        "description": null
    },
    "id": "/subscriptions/{subscriptionId1}/providers/Microsoft.Authorization/roleAssignments/{roleAssignmentId1}",
    "type": "Microsoft.Authorization/roleAssignments",
    "name": "{roleAssignmentId1}"
}

ARM 模板

无法使用 Azure 资源管理器模板(ARM 模板)来删除角色分配。 若要删除角色分配,必须使用其他工具,如 Azure 门户、Azure PowerShell、Azure CLI 或 REST API。

后续步骤