Bewerken

Delen via


Customize claims with the claims mapping policy in Microsoft Graph

You can add extra user attributes to access tokens to help your app make better authorization decisions. This article shows how to use Microsoft Graph APIs to create and assign a claims mapping policy, add custom claims to access tokens, and verify the custom claim in the token.

Prerequisites

To complete this tutorial, you need:

  • Access to an API client such as Graph Explorer, signed in with a Microsoft Entra account that has the Application Administrator role and grant the following delegated permissions: Policy.Read.All, Policy.ReadWrite.ApplicationConfiguration, and Application.ReadWrite.All.
  • A client service principal to assign the claims mapping policy to.
  • A resource service principal that exposes APIs.

Create a claims mapping policy

This policy adds the department claim from the user object to the token.

Request

POST https://graph.microsoft.com/beta/policies/claimsMappingPolicies
Content-type: application/json

{
 "definition": [
   "{\"ClaimsMappingPolicy\":{\"Version\":1,\"IncludeBasicClaimSet\":\"true\",\"ClaimsSchema\":[{\"Source\":\"user\",\"ID\":\"department\",\"JwtClaimType\":\"department\"}]}}"
 ],
 "displayName": "ExtraClaimsTest"
}

Response

Record the ID from the response to use later in this article.

HTTP/1.1 201 Created
Content-type: application/json

{
  "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#policies/claimsMappingPolicies/$entity",
  "id": "06d5d20d-2955-45f8-a15d-cf2f434b8116",
  "deletedDateTime": null,
  "definition": [
      "{\"ClaimsMappingPolicy\":{\"Version\":1,\"IncludeBasicClaimSet\":\"true\",\"ClaimsSchema\":[{\"Source\":\"user\",\"ID\":\"department\",\"JwtClaimType\":\"department\"}]}}"
  ],
  "displayName": "ExtraClaimsTest",
  "isOrganizationDefault": false
}

You can also add more than one attribute to the policy. The following sample adds both the department and companyname claims to the token.

{
  "definition": [
        "{\"ClaimsMappingPolicy\":{\"Version\":1,\"IncludeBasicClaimSet\":\"true\",\"ClaimsSchema\":[{\"Source\":\"user\",\"ID\":\"department\",\"JwtClaimType\":\"department\"},{\"Source\":\"user\",\"ID\":\"companyname\",\"JwtClaimType\":\"companyname\"}]}}"
    ],
 "displayName": "ExtraClaimsTest"
}

Assign the policy to a resource service principal

The following request assigns the claims mapping policy to a service principal. A successful response returns 204 No Content.

POST https://graph.microsoft.com/v1.0/servicePrincipals/3bdbbc1a-5e94-4c2b-895f-231d8af4beee/claimsMappingPolicies/$ref
Content-type: application/json

{
 "@odata.id": "https://graph.microsoft.com/v1.0/policies/claimsMappingPolicies/06d5d20d-2955-45f8-a15d-cf2f434b8116"
}

Enable mapped claims in the resource application object

Update the application object to accept mapped claims and use access token version 2. A successful response returns 204 No Content.

PATCH https://graph.microsoft.com/v1.0/applications/3dfbe85f-2d14-4660-b1a2-cb9c633ceebb
Content-type: application/json

{
  "api": {
    "acceptMappedClaims": true,
    "requestedAccessTokenVersion": 2
  }
}

Test the access token

In an API client that allows you to follow the Microsoft identity platform and OAuth 2.0 authorization code flow, obtain an access token. In the scope parameter, include one of the scopes exposed by your resource service principal, such as openid profile email scope-defined-by-your-api where scope-defined-by-your-api might be api://00001111-aaaa-2222-bbbb-3333cccc4444/test.

Use jwt.ms to decode the access token. The department claim should appear in the token.

Clean up resources

To unassign the claims mapping policy from the service principal, use the following request. A successful response returns 204 No Content.

DELETE https://graph.microsoft.com/v1.0/servicePrincipals/3bdbbc1a-5e94-4c2b-895f-231d8af4beee/claimsMappingPolicies//06d5d20d-2955-45f8-a15d-cf2f434b8116/$ref