question

AlexanderKlimuk-6225 avatar image
0 Votes"
AlexanderKlimuk-6225 asked HugoPedrosa-8590 edited

How to enrich azure b2c token with custom claims using api connectors and asp net core web api

I have a user flow B2C_1_singupsingin1 I added an api connector, embed it in this stream and the endpoint url for the API call. Used article: https://docs.microsoft.com/en-us/azure/active-directory-b2c/add-api-connector-token-enrichment?pivots=b2c-user-flow

It is clear from the article that the API connector materializes as an HTTP POST request, sending custom attributes.

My web api has an endpoint with the code:


 [HttpPost("enrich")]
 public IActionResult Enrich([FromBody] JsonElement body)
 {
     var responseProperties = new Dictionary<string, object> //for example
     {
         { "version", "1.0.0" },
         { "action", "Continue" },
         { "postalCode", "12349" },
         { "userId", 123 } 
     };
    
     return new JsonResult(responseProperties) { StatusCode = 200 };
 }


When I start a custom flow everything works, I get to that endpoint in api. But there is a problem JsonElement body does't contain custom attributes. Inside I see body.ValueKind = Undefined. Tell me what am I doing wrong?

Also, after all, I wanted to add a custom "userId" claim with some value from my database. So that it is contained in the token issued in the subsequent. Would the code above be correct for this?



dotnet-csharpazure-ad-b2c
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

HugoPedrosa-8590 avatar image
0 Votes"
HugoPedrosa-8590 answered HugoPedrosa-8590 edited

I had the same problem.

You need to replace "JsonElement" by "JObject"

and you can get your objectId using this
var objectId = body.GetValue("objectId").ToString();

  [HttpPost("enrich")]
  public IActionResult Enrich([FromBody] JObject body)
  {
       // Get the object id of the user that is signing in.
       var objectId = body.GetValue("objectId").ToString();
    
      var responseProperties = new Dictionary<string, object> //for example
      {
          { "version", "1.0.0" },
          { "action", "Continue" },
          { "postalCode", "12349" },
          { "userId", 123 } 
      };
        
      return new JsonResult(responseProperties) { StatusCode = 200 };
  }


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.