Grant an appRoleAssignment for a service principal
Artikel
Namespace: microsoft.graph
Assign an app role for a resource service principal, to a user, group, or 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, you need three identifiers:
principalId: The id of the user, group or client servicePrincipal to which you are assigning the app role.
resourceId: The id of the resource servicePrincipal which has defined the app role.
appRoleId: The id of the appRole (defined on the resource service principal) to assign to a user, group, or service principal.
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 and Application.Read.All
AppRoleAssignment.ReadWrite.All and Directory.Read.All
Delegated (personal Microsoft account)
Not supported.
Not supported.
Application
AppRoleAssignment.ReadWrite.All and Application.Read.All
AppRoleAssignment.ReadWrite.All and Directory.Read.All
For delegated scenarios, the calling user needs at least one of the following Microsoft Entra roles.
Directory Synchronization Accounts
Directory Writer
Hybrid Identity Administrator
Identity Governance Administrator
Privileged Role Administrator
User Administrator
Application Administrator
Cloud Application Administrator
HTTP request
You can address the service principal using either its id or appId. id and appId are referred to as the Object ID and Application (Client) ID, respectively, in app registrations in the Microsoft Entra admin center.
POST /servicePrincipals/{id}/appRoleAssignedTo
POST /servicePrincipals(appId='{appId}')/appRoleAssignedTo
// 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("33ad69f9-da99-4bed-acd0-3f24235cb296"),
ResourceId = Guid.Parse("9028d19c-26a9-4809-8e3f-20ff73e2d75e"),
AppRoleId = Guid.Parse("ef7437e6-4f94-4a0a-a110-a439eb2aa8f7"),
};
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
var result = await graphClient.ServicePrincipals["{servicePrincipal-id}"].AppRoleAssignedTo.PostAsync(requestBody);
// THE CLI IS IN PREVIEW. NON-PRODUCTION USE ONLY
mgc service-principals app-role-assigned-to create --service-principal-id {servicePrincipal-id} --body '{\
"principalId": "33ad69f9-da99-4bed-acd0-3f24235cb296",\
"resourceId": "9028d19c-26a9-4809-8e3f-20ff73e2d75e",\
"appRoleId": "ef7437e6-4f94-4a0a-a110-a439eb2aa8f7"\
}\
'
// 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("33ad69f9-da99-4bed-acd0-3f24235cb296"));
appRoleAssignment.setResourceId(UUID.fromString("9028d19c-26a9-4809-8e3f-20ff73e2d75e"));
appRoleAssignment.setAppRoleId(UUID.fromString("ef7437e6-4f94-4a0a-a110-a439eb2aa8f7"));
AppRoleAssignment result = graphClient.servicePrincipals().byServicePrincipalId("{servicePrincipal-id}").appRoleAssignedTo().post(appRoleAssignment);
<?php
use Microsoft\Graph\GraphServiceClient;
use Microsoft\Graph\Generated\Models\AppRoleAssignment;
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
$requestBody = new AppRoleAssignment();
$requestBody->setPrincipalId('33ad69f9-da99-4bed-acd0-3f24235cb296');
$requestBody->setResourceId('9028d19c-26a9-4809-8e3f-20ff73e2d75e');
$requestBody->setAppRoleId('ef7437e6-4f94-4a0a-a110-a439eb2aa8f7');
$result = $graphServiceClient->servicePrincipals()->byServicePrincipalId('servicePrincipal-id')->appRoleAssignedTo()->post($requestBody)->wait();
In this example, {id} and {resourceId-value} would both be the id of the resource service principal, and {principalId} would be the id of the assigned user, group, or client service principal.
Response
The following example shows the response.
Note: The response object shown here might be shortened for readability.
Segera hadir: Sepanjang tahun 2024 kami akan menghentikan penggunaan GitHub Issues sebagai mekanisme umpan balik untuk konten dan menggantinya dengan sistem umpan balik baru. Untuk mengetahui informasi selengkapnya, lihat: https://aka.ms/ContentUserFeedback.