TransformationClaimType="personalizations.0.to.0.email" is not parsing special characters in adb2c custom policy

Harshal Wankhade 0 Reputation points
2023-03-22T15:31:40.65+00:00

We have used Azure Active Directory B2C(adb2c) custom policy for sign in flow.

We want to read email address and create json string so that we can send the json string to SendGrid for sending an email

But while parsing the email with special character we are getting the below error:

Exception Message:After parsing a value an unexpected character was encountered: D. Path 'personalizations[0].to[0].email', line 1, position 49., Exception Type:InvalidResponseException,

We are using the below claim transformation for parsing the email address:

<InputClaim ClaimTypeReferenceId="readOnlyEmail" TransformationClaimType="personalizations.0.to.0.email" />

email address we are trying to pass on is: jack.O'Donel@gmail.com

Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
21,292 questions
{count} votes

1 answer

Sort by: Most helpful
  1. James Hamil 24,136 Reputation points Microsoft Employee
    2023-03-22T20:34:19.5633333+00:00

    Hi @Harshal Wankhade , the error message indicates that the JSON parser encountered an unexpected character, which is the apostrophe in the email address. To fix this issue, you need to escape the apostrophe character in the email address before passing it to the JSON parser.

    You can use the Replace claims transformation to replace the apostrophe character with its escaped version. Here's an example of how to use the Replace claims transformation to escape the apostrophe character:

    <ClaimsTransformation Id="EscapeApostrophe" TransformationMethod="Replace">
      <InputClaims>
        <InputClaim ClaimTypeReferenceId="readOnlyEmail" TransformationClaimType="unescapedEmail" />
      </InputClaims>
      <InputParameters>
        <InputParameter Id="find" DataType="string" Value="'" />
        <InputParameter Id="replace" DataType="string" Value="\'" />
      </InputParameters>
      <OutputClaims>
        <OutputClaim ClaimTypeReferenceId="escapedEmail" TransformationClaimType="outputClaim" />
      </OutputClaims>
    </ClaimsTransformation>
    
    

    This claims transformation takes the readOnlyEmail claim as input, replaces the apostrophe character with its escaped version (\'), and outputs the result in the escapedEmail claim. You can then use the escapedEmail claim in your JSON string.

    Here's an example of how to use the GenerateJson claims transformation to create a JSON string with the email address:

    
    <ClaimsTransformation Id="GenerateRequestBody" TransformationMethod="GenerateJson">
      <InputClaims>
        <InputClaim ClaimTypeReferenceId="escapedEmail" TransformationClaimType="personalizations.0.to.0.email" />
        <InputClaim ClaimTypeReferenceId="otp" TransformationClaimType="personalizations.0.dynamic_template_data.otp" />
        <InputClaim ClaimTypeReferenceId="copiedEmail" TransformationClaimType="personalizations.0.dynamic_template_data.verify-email" />
      </InputClaims>
      <InputParameters>
        <InputParameter Id="template_id" DataType="string" Value="d-4c56ffb40fa648b1aa6822283df94f60" />
        <InputParameter Id="from.email" DataType="string" Value="service@contoso.com" />
        <InputParameter Id="personalizations.0.subject" DataType="string" Value="Contoso account email verification code" />
      </InputParameters>
      <OutputClaims>
        <OutputClaim ClaimTypeReferenceId="requestBody" TransformationClaimType="outputClaim" />
      </OutputClaims>
    </ClaimsTransformation>
    
    

    This claims transformation takes the escapedEmail, otp, and copiedEmail claims as input, and generates a JSON string with these values. You can then use the requestBody claim in your SendGrid request.

    Please note that the Replace claims transformation may not be sufficient to escape all special characters in an email address. You may need to implement a more robust solution to handle all possible special characters.

    Please let me know if you have any questions and I can help you further.

    If this answer helped you please mark it as "Verified" so other users can reference it.

    Thank you,

    James

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.