Anteckning
Åtkomst till den här sidan kräver auktorisering. Du kan prova att logga in eller ändra kataloger.
Åtkomst till den här sidan kräver auktorisering. Du kan prova att ändra kataloger.
Rollbaserad åtkomstkontroll i Azure (Azure RBAC) är det auktoriseringssystem som du använder för att hantera åtkomst till Azure-resurser. Om du vill bevilja åtkomst tilldelar du roller till användare, grupper, tjänsthuvudnamn eller hanterade identiteter i ett visst omfång. This article describes how to assign roles using the REST API.
Förutsättningar
Om du vill tilldela Azure-roller måste du ha:
- 
              Microsoft.Authorization/roleAssignments/writebehörigheter, till exempel administratör för rollbaserad åtkomstkontroll eller administratör för användaråtkomst
Du måste använda följande versioner:
- 
              2015-07-01or later to assign an Azure role
- 
              2018-09-01-previewor later to assign an Azure role to a new service principal
Mer information finns i API-versioner av Azure RBAC REST API:er.
Assign an Azure role
To assign a role, use the Role Assignments - Create REST API and specify the security principal, role definition, and scope. To call this API, you must have access to the Microsoft.Authorization/roleAssignments/write action, such as Role Based Access Control Administrator.
- Use the Role Definitions - List REST API or see Built-in roles to get the identifier for the role definition you want to assign. 
- Use a GUID tool to generate a unique identifier that will be used for the role assignment identifier. The identifier has the format: - 00000000-0000-0000-0000-000000000000
- Start with the following request and body: - PUT https://management.azure.com/{scope}/providers/Microsoft.Authorization/roleAssignments/{roleAssignmentId}?api-version=2022-04-01- { "properties": { "roleDefinitionId": "/{scope}/providers/Microsoft.Authorization/roleDefinitions/{roleDefinitionId}", "principalId": "{principalId}" } }
- Within the URI, replace {scope} with the scope for the role assignment. - Definitionsområde - Typ - providers/Microsoft.Management/managementGroups/{groupId1}- Hanteringsgrupp - subscriptions/{subscriptionId1}- Prenumeration - subscriptions/{subscriptionId1}/resourceGroups/myresourcegroup1- Resursgrupp - subscriptions/{subscriptionId1}/resourceGroups/myresourcegroup1/providers/microsoft.web/sites/mysite1- Resurs - I föregående exempel är microsoft.web en resursprovider som refererar till en App Service-instans. På samma sätt kan du använda andra resursproviders och ange omfånget. Mer information finns i Azure-resursleverantörer och typer och Azure-resursleverantörsoperationer som stöds. 
- Replace {roleAssignmentId} with the GUID identifier of the role assignment. 
- Within the request body, replace {scope} with the same scope as in the URI. 
- Replace {roleDefinitionId} with the role definition identifier. 
- Replace {principalId} with the object identifier of the user, group, or service principal that will be assigned the role. 
The following request and body assigns the Backup Reader role to a user at subscription scope:
PUT https://management.azure.com/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}"
  }
}
Följande visar ett exempel på utdata:
{
    "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:23.7679147Z",
        "updatedOn": "2022-05-06T23:55:23.7679147Z",
        "createdBy": null,
        "updatedBy": "{updatedByObjectId1}",
        "delegatedManagedIdentityResourceId": null,
        "description": null
    },
    "id": "/subscriptions/{subscriptionId1}/providers/Microsoft.Authorization/roleAssignments/{roleAssignmentId1}",
    "type": "Microsoft.Authorization/roleAssignments",
    "name": "{roleAssignmentId1}"
}
Nytt huvudnamn för tjänsten
Om du skapar ett nytt huvudnamn för tjänsten och omedelbart försöker tilldela tjänstens huvudnamn en roll kan rolltilldelningen misslyckas i vissa fall. For example, if you create a new managed identity and then try to assign a role to that service principal, the role assignment might fail. Orsaken till det här felet är sannolikt en replikeringsfördröjning. Tjänstens huvudnamn skapas i en region. Rolltilldelningen kan dock ske i en annan region som inte har replikerat tjänstens huvudnamn ännu.
To address this scenario, use the Role Assignments - Create REST API and set the principalType property to ServicePrincipal. You must also set the apiVersion to 2018-09-01-preview or later. 
              2022-04-01 är den första stabila versionen.
PUT https://management.azure.com/{scope}/providers/Microsoft.Authorization/roleAssignments/{roleAssignmentId}?api-version=2022-04-01
{
  "properties": {
    "roleDefinitionId": "/{scope}/providers/Microsoft.Authorization/roleDefinitions/{roleDefinitionId}",
    "principalId": "{principalId}",
    "principalType": "ServicePrincipal"
  }
}