Custom AD B2C policy to add a User to a group

Robert Florian Raicu 21 Reputation points

I would like to add a user to a group using a custom policy.

I have this claim:
<ClaimType Id="graphUserUri">

Which I use it with this transformation:

  <ClaimsTransformation Id="REST-AddToFreelancersGroup-UserUri" TransformationMethod="FormatStringClaim">  
          <InputClaim ClaimTypeReferenceId="objectId" TransformationClaimType="inputClaim" />  
          <InputParameter Id="stringFormat" DataType="string" Value='{{  " " : "{0}" }}' />  
          <OutputClaim ClaimTypeReferenceId="graphUserUri" TransformationClaimType="outputClaim" />  

I get a token for the graph api using:

<TechnicalProfile Id="REST-AcquireAccessTokenForGraph">  
          <DisplayName>Acquire Token</DisplayName>  
          <Protocol Name="Proprietary" Handler="Web.TPEngine.Providers.RestfulProvider, Web.TPEngine, Version=, Culture=neutral, PublicKeyToken=null" />  
            <Item Key="ServiceUrl"></Item>  
            <Item Key="AuthenticationType">Basic</Item>  
            <Item Key="SendClaimsIn">Form</Item>  
            <Key Id="BasicAuthenticationUsername" StorageReferenceId="B2C_1A_MSGraphClientId" />  
            <Key Id="BasicAuthenticationPassword" StorageReferenceId="B2C_1A_MSGraphClientSecret" />  
            <InputClaim ClaimTypeReferenceId="grant_type" DefaultValue="client_credentials" />  
            <InputClaim ClaimTypeReferenceId="scope" DefaultValue="" />  
            <OutputClaim ClaimTypeReferenceId="bearerToken" PartnerClaimType="access_token" />  
          <UseTechnicalProfileForSessionManagement ReferenceId="SM-Noop" />  

And I make a call to the graph api to add the user to a group:

        <TechnicalProfile Id="REST-AddToFreelancersGroup">  
          <DisplayName>Add user to group</DisplayName>  
          <Protocol Name="Proprietary" Handler="Web.TPEngine.Providers.RestfulProvider, Web.TPEngine, Version=, Culture=neutral, PublicKeyToken=null" />  
            <Item Key="ServiceUrl">$ref</Item>  
            <Item Key="SendClaimsIn">Body</Item>  
            <Item Key="ClaimUsedForRequestPayload">graphUserUri</Item>  
            <Item Key="AuthenticationType">Bearer</Item>  
            <Item Key="UseClaimAsBearerToken">bearerToken</Item>  
            <Item Key="DebugMode">true</Item>  
            <InputClaimsTransformation ReferenceId="REST-AddToFreelancersGroup-UserUri"/>  
            <InputClaim ClaimTypeReferenceId="objectId" />   
            <InputClaim ClaimTypeReferenceId="graphUserUri" />   
            <InputClaim ClaimTypeReferenceId="bearerToken" />   
            <OutputClaim ClaimTypeReferenceId="err" />  
          <UseTechnicalProfileForSessionManagement ReferenceId="SM-Noop" />  

All these steps work perfectly in PostMan but as a custom policy they end up in a Bad Request.
My issue is with Custom Policies and Application Insights.
The logs are limited at 30k characters and they terminate right at the important part

The next log available is just stating that the error happened without further details.


The generated claim inside the logs looks fine:
"graphUserUri": "{ \" \" : \"\" }"
and I tested the generated bearer token in postman and it works.

The issue seems to be with the call to the API but due to the lack of logs I can't figure it out.
I've tried to enable all the debugging features of the custom policies including
<Item Key="DebugMode">true</Item> for the techincal profile and


for the TrustFrameworkPolicy

I'm happy to offer additional details if asked.

Microsoft Entra External ID
Microsoft Entra External ID
A modern identity solution for securing access to customer, citizen and partner-facing apps and services. It is the converged platform of Azure AD External Identities B2B and B2C. Replaces Azure Active Directory External Identities.
2,770 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
20,601 questions
0 comments No comments
{count} votes

Accepted answer
  1. 2022-11-16T21:38:36.72+00:00

    Hello @Robert Florian Raicu and thanks for reaching out. To better inspect data from both Azure AD B2C request and MS Graph response place create an ngrok HTTP tunnel to MS Graph and target it from your custom policies. You can inspect requests and responses using the ngrok Web Inspection Interface:

    1. Set up the tunnel: ngrok http --log-level debug
    2. In your custom policy: Replace{0} with https://ngrok public facing url/v1.0/users/{0}.
    3. Call the custom policy and inspect the ngrok web interface. E.g.

    Let us know if you need additional assistance. If the answer was helpful, please accept it and complete the quality survey so that others can find a solution.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful