Microsoft Docs

Your guide to Azure and developer technologies

Adding Custom Claims to Azure AD B2C Tokens

Enhancing security and providing richer user context in your applications often requires including specific, custom information within the authentication tokens issued by Azure Active Directory B2C (Azure AD B2C). This guide will walk you through the process of defining and including custom claims in your ID and Access tokens.

Why Use Custom Claims?

Standard claims like user ID, name, and email are fundamental. However, you might need to send additional data to your application for various purposes:

Important: Custom claims are embedded directly into the token. Be mindful of token size limitations and the sensitivity of the data you are including. Avoid storing highly sensitive personal information that doesn't need to be readily available in the token.

Steps to Add Custom Claims

1. Define Custom Attributes

Before you can add custom claims to tokens, you need to define them as custom attributes in your Azure AD B2C directory. These attributes will store the data associated with your users.

  1. Navigate to your Azure AD B2C tenant in the Azure portal.
  2. Under "User attributes", select "Add".
  3. Provide a descriptive name for your attribute (e.g., extension_userRoles, extension_tenantId). Azure AD B2C automatically prefixes custom attributes with extension_.
  4. Choose the data type (e.g., String, Boolean, Integer).
  5. Click "Create".

2. Populate Custom Attributes

You can populate these custom attributes in several ways:

3. Configure Token Claims in User Flows or Custom Policies

The method for adding custom claims to tokens depends on whether you are using Azure AD B2C's built-in user flows or custom policies.

Using User Flows (Recommended for simplicity)

User flows offer a straightforward way to configure token claims.

  1. In your Azure AD B2C tenant, navigate to "User flows".
  2. Select the user flow you want to configure (e.g., your sign-up/sign-in flow).
  3. Under "Settings", click on "Properties".
  4. Scroll down to the "Token customization" section.
  5. Click "Edit".
  6. Under "User attributes", select the custom attributes you wish to include in the token.
  7. Click "Save".

Example: If you created an attribute named extension_userRoles, select it from the list. It will appear in the token as userRoles.

Using Custom Policies (For advanced scenarios)

Custom policies provide maximum flexibility but involve editing XML files.

You'll need to define a TechnicalProfile that represents the claims you want to output. This typically involves referencing your custom attributes.

In your Relying Party (RP) file, within the OutputClaims section of your token issuing TechnicalProfile, add a new OutputClaim element:

<OutputClaim ClaimType="userRoles" PartnerClaimType="roles" Required="true" />

Here:

You will also need to ensure that the custom attribute itself is correctly referenced and claims-transformed within your custom policy's identity provider or technical profiles. For instance, mapping the user's data from Azure AD B2C to this claim type.

Referencing Custom Attributes in Custom Policies: When using custom policies, ensure that the TechnicalProfile that reads user data (e.g., an Active Directory technical profile) correctly includes the custom attribute you wish to expose. You might need to add it to the OutputClaims of that technical profile and then map it to the desired ClaimType in your RP file.

4. Verify Custom Claims

After configuring your user flow or custom policy, test your sign-up or sign-in process. You can inspect the generated ID or Access tokens using browser developer tools or online JWT token decoders to verify that your custom claims are present and populated correctly.

Best Practices

By effectively leveraging custom claims, you can build more secure, personalized, and context-aware applications with Azure AD B2C.