Grant an appRoleAssignment to a service principal
Article
03/19/2022
3 minutes to read
9 contributors
In this article
Namespace: microsoft.graph
Assign an app role to a client service principal.
App roles that are assigned to service principals are also known as application permissions . Application permissions can be granted directly with app role assignments, or through a consent experience .
To grant an app role assignment to a client service principal, you need three identifiers:
principalId
: The id
of the client service principal to which you are assigning the app role.
resourceId
: The id
of the resource servicePrincipal
(the API) which has defined the app role (the application permission).
appRoleId
: The id
of the appRole
(defined on the resource service principal) to assign to the client service principal.
Permissions
One of the following permissions is required to call this API. To learn more, including how to choose permissions, see Permissions .
Permission type
Permissions (from least to most privileged)
Delegated (work or school account)
AppRoleAssignment.ReadWrite.All, Directory.ReadWrite.All
Delegated (personal Microsoft account)
Not supported.
Application
AppRoleAssignment.ReadWrite.All, Directory.ReadWrite.All
HTTP request
POST /servicePrincipals/{id}/appRoleAssignments
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.
Examples
Request
Here is an example of the request.
POST https://graph.microsoft.com/v1.0/servicePrincipals/9028d19c-26a9-4809-8e3f-20ff73e2d75e/appRoleAssignments
Content-Type: application/json
{
"principalId": "9028d19c-26a9-4809-8e3f-20ff73e2d75e",
"resourceId": "8fce32da-1246-437b-99cd-76d1d4677bd5",
"appRoleId": "498476ce-e0fe-48b0-b801-37ba7e2685c6"
}
GraphServiceClient graphClient = new GraphServiceClient( authProvider );
var appRoleAssignment = new AppRoleAssignment
{
PrincipalId = Guid.Parse("9028d19c-26a9-4809-8e3f-20ff73e2d75e"),
ResourceId = Guid.Parse("8fce32da-1246-437b-99cd-76d1d4677bd5"),
AppRoleId = Guid.Parse("498476ce-e0fe-48b0-b801-37ba7e2685c6")
};
await graphClient.ServicePrincipals["{servicePrincipal-id}"].AppRoleAssignments
.Request()
.AddAsync(appRoleAssignment);
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation .
const options = {
authProvider,
};
const client = Client.init(options);
const appRoleAssignment = {
principalId: '9028d19c-26a9-4809-8e3f-20ff73e2d75e',
resourceId: '8fce32da-1246-437b-99cd-76d1d4677bd5',
appRoleId: '498476ce-e0fe-48b0-b801-37ba7e2685c6'
};
await client.api('/servicePrincipals/9028d19c-26a9-4809-8e3f-20ff73e2d75e/appRoleAssignments')
.post(appRoleAssignment);
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation .
MSHTTPClient *httpClient = [MSClientFactory createHTTPClientWithAuthenticationProvider:authenticationProvider];
NSString *MSGraphBaseURL = @"https://graph.microsoft.com/v1.0/";
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[MSGraphBaseURL stringByAppendingString:@"/servicePrincipals/9028d19c-26a9-4809-8e3f-20ff73e2d75e/appRoleAssignments"]]];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
MSGraphAppRoleAssignment *appRoleAssignment = [[MSGraphAppRoleAssignment alloc] init];
[appRoleAssignment setPrincipalId:@"9028d19c-26a9-4809-8e3f-20ff73e2d75e"];
[appRoleAssignment setResourceId:@"8fce32da-1246-437b-99cd-76d1d4677bd5"];
[appRoleAssignment setAppRoleId:@"498476ce-e0fe-48b0-b801-37ba7e2685c6"];
NSError *error;
NSData *appRoleAssignmentData = [appRoleAssignment getSerializedDataWithError:&error];
[urlRequest setHTTPBody:appRoleAssignmentData];
MSURLSessionDataTask *meDataTask = [httpClient dataTaskWithRequest:urlRequest
completionHandler: ^(NSData *data, NSURLResponse *response, NSError *nserror) {
//Request Completed
}];
[meDataTask execute];
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation .
GraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider( authProvider ).buildClient();
AppRoleAssignment appRoleAssignment = new AppRoleAssignment();
appRoleAssignment.principalId = UUID.fromString("9028d19c-26a9-4809-8e3f-20ff73e2d75e");
appRoleAssignment.resourceId = UUID.fromString("8fce32da-1246-437b-99cd-76d1d4677bd5");
appRoleAssignment.appRoleId = UUID.fromString("498476ce-e0fe-48b0-b801-37ba7e2685c6");
graphClient.servicePrincipals("9028d19c-26a9-4809-8e3f-20ff73e2d75e").appRoleAssignments()
.buildRequest()
.post(appRoleAssignment);
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation .
//THE GO SDK IS IN PREVIEW. NON-PRODUCTION USE ONLY
graphClient := msgraphsdk.NewGraphServiceClient(requestAdapter)
requestBody := msgraphsdk.NewAppRoleAssignment()
principalId := "9028d19c-26a9-4809-8e3f-20ff73e2d75e"
requestBody.SetPrincipalId(&principalId)
resourceId := "8fce32da-1246-437b-99cd-76d1d4677bd5"
requestBody.SetResourceId(&resourceId)
appRoleId := "498476ce-e0fe-48b0-b801-37ba7e2685c6"
requestBody.SetAppRoleId(&appRoleId)
servicePrincipalId := "servicePrincipal-id"
result, err := graphClient.ServicePrincipalsById(&servicePrincipalId).AppRoleAssignments().Post(requestBody)
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation .
Import-Module Microsoft.Graph.Applications
$params = @{
PrincipalId = "9028d19c-26a9-4809-8e3f-20ff73e2d75e"
ResourceId = "8fce32da-1246-437b-99cd-76d1d4677bd5"
AppRoleId = "498476ce-e0fe-48b0-b801-37ba7e2685c6"
}
New-MgServicePrincipalAppRoleAssignment -ServicePrincipalId $servicePrincipalId -BodyParameter $params
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation .
In this example, note that the value used as the service principal id in the request URL (9028d19c-26a9-4809-8e3f-20ff73e2d75e
) is the same as the principalId property in the body. The resourceId value is the id of the resource service principal (the API).
Response
Here is an example of the response.
Note: The response object shown here might be shortened for readability.
HTTP/1.1 201 Created
Content-type: application/json
{
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#appRoleAssignments/$entity",
"id": "2jLOj0YSe0OZzXbR1Gd71fDqFUrPM1xIgUfvWBHJ9n0",
"createdDateTime": "2021-02-15T16:39:38.2975029Z",
"appRoleId": "498476ce-e0fe-48b0-b801-37ba7e2685c6",
"principalDisplayName": "Fabrikam App",
"principalId": "9028d19c-26a9-4809-8e3f-20ff73e2d75e",
"principalType": "ServicePrincipal",
"resourceDisplayName": "Microsoft Graph",
"resourceId": "8fce32da-1246-437b-99cd-76d1d4677bd5"
}