Choose the permission or permissions marked as least privileged for this API. Use a higher privileged permission or permissions only if your app requires it. For details about delegated and application permissions, see Permission types. To learn more about these permissions, see the permissions reference.
Permission type
Least privileged permissions
Higher privileged permissions
Delegated (work or school account)
AppRoleAssignment.ReadWrite.All
Not available.
Delegated (personal Microsoft account)
Not supported.
Not supported.
Application
AppRoleAssignment.ReadWrite.All
Not available.
HTTP request
POST /users/{id | userPrincipalName}/appRoleAssignments
Note
As a best practice, we recommend creating app role assignments through the appRoleAssignedTo relationship of the resource service principal, instead of the appRoleAssignments relationship of the assigned user, group, or service principal.
Request headers
Name
Description
Authorization
Bearer {token}. Required.
Content-Type
application/json. Required.
Request body
In the request body, supply a JSON representation of an appRoleAssignment object.
Response
If successful, this method returns a 201 Created response code and an appRoleAssignment object in the response body.
// Code snippets are only available for the latest version. Current version is 5.x
// Dependencies
using Microsoft.Graph.Models;
var requestBody = new AppRoleAssignment
{
PrincipalId = Guid.Parse("cde330e5-2150-4c11-9c5b-14bfdc948c79"),
ResourceId = Guid.Parse("8e881353-1735-45af-af21-ee1344582a4d"),
AppRoleId = Guid.Parse("00000000-0000-0000-0000-000000000000"),
};
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
var result = await graphClient.Users["{user-id}"].AppRoleAssignments.PostAsync(requestBody);
// THE CLI IS IN PREVIEW. NON-PRODUCTION USE ONLY
mgc users app-role-assignments create --user-id {user-id} --body '{\
"principalId": "cde330e5-2150-4c11-9c5b-14bfdc948c79",\
"resourceId": "8e881353-1735-45af-af21-ee1344582a4d",\
"appRoleId": "00000000-0000-0000-0000-000000000000"\
}\
'
<?php
// THIS SNIPPET IS A PREVIEW VERSION OF THE SDK. NON-PRODUCTION USE ONLY
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
$requestBody = new AppRoleAssignment();
$requestBody->setPrincipalId('cde330e5-2150-4c11-9c5b-14bfdc948c79');
$requestBody->setResourceId('8e881353-1735-45af-af21-ee1344582a4d');
$requestBody->setAppRoleId('00000000-0000-0000-0000-000000000000');
$result = $graphServiceClient->users()->byUserId('user-id')->appRoleAssignments()->post($requestBody)->wait();
# THE PYTHON SDK IS IN PREVIEW. FOR NON-PRODUCTION USE ONLY
graph_client = GraphServiceClient(credentials, scopes)
request_body = AppRoleAssignment(
principal_id = UUID("cde330e5-2150-4c11-9c5b-14bfdc948c79"),
resource_id = UUID("8e881353-1735-45af-af21-ee1344582a4d"),
app_role_id = UUID("00000000-0000-0000-0000-000000000000"),
)
result = await graph_client.users.by_user_id('user-id').app_role_assignments.post(request_body)
In this example, note that the value used as the user id in the request URL (cde330e5-2150-4c11-9c5b-14bfdc948c79) is the same as the principalId property in the body.
Response
Here is an example of the response.
Note: The response object shown here might be shortened for readability.