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.
Important
In delegated scenarios with work or school accounts, the signed-in user must be assigned a supported Microsoft Entra role or a custom role with a supported role permission. The following least privileged roles are supported for this operation:
Directory Synchronization Accounts - for Microsoft Entra Connect and Microsoft Entra Cloud Sync services
Directory Writer
Hybrid Identity Administrator
Identity Governance Administrator
Privileged Role Administrator
User Administrator
Application Administrator
Cloud Application Administrator
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.
// 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://video2.skills-academy.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
var result = await graphClient.Users["{user-id}"].AppRoleAssignments.PostAsync(requestBody);
// Code snippets are only available for the latest major version. Current major version is $v1.*
// Dependencies
import (
"context"
"github.com/google/uuid"
msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go"
graphmodels "github.com/microsoftgraph/msgraph-sdk-go/models"
//other-imports
)
requestBody := graphmodels.NewAppRoleAssignment()
principalId := uuid.MustParse("cde330e5-2150-4c11-9c5b-14bfdc948c79")
requestBody.SetPrincipalId(&principalId)
resourceId := uuid.MustParse("8e881353-1735-45af-af21-ee1344582a4d")
requestBody.SetResourceId(&resourceId)
appRoleId := uuid.MustParse("00000000-0000-0000-0000-000000000000")
requestBody.SetAppRoleId(&appRoleId)
// To initialize your graphClient, see https://video2.skills-academy.com/en-us/graph/sdks/create-client?from=snippets&tabs=go
appRoleAssignments, err := graphClient.Users().ByUserId("user-id").AppRoleAssignments().Post(context.Background(), requestBody, nil)
// Code snippets are only available for the latest version. Current version is 6.x
GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
AppRoleAssignment appRoleAssignment = new AppRoleAssignment();
appRoleAssignment.setPrincipalId(UUID.fromString("cde330e5-2150-4c11-9c5b-14bfdc948c79"));
appRoleAssignment.setResourceId(UUID.fromString("8e881353-1735-45af-af21-ee1344582a4d"));
appRoleAssignment.setAppRoleId(UUID.fromString("00000000-0000-0000-0000-000000000000"));
AppRoleAssignment result = graphClient.users().byUserId("{user-id}").appRoleAssignments().post(appRoleAssignment);
<?php
use Microsoft\Graph\GraphServiceClient;
use Microsoft\Graph\Generated\Models\AppRoleAssignment;
$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();
# Code snippets are only available for the latest version. Current version is 1.x
from msgraph import GraphServiceClient
from msgraph.generated.models.app_role_assignment import AppRoleAssignment
# To initialize your graph_client, see https://video2.skills-academy.com/en-us/graph/sdks/create-client?from=snippets&tabs=python
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
The following example shows the response.
Note: The response object shown here might be shortened for readability.