3

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://learn.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?

  • I don't think it works. It's in preview, I've got the same situation. The audit logs in B2C show the continuation and its marked as success, but the claim is not added to the token. – Matt G Oct 26 '21 at 22:38

2 Answers2

5

Your code is fine. Just add "extension_" in front of postalCode and userId.

    [HttpPost("log")]
    public IActionResult Log([FromBody] JsonElement body)
    {

        var responseProperties = new Dictionary<string, object> 
        {
            { "version", "1.0.0" },
            { "action", "Continue" },
            { "extension_Role", "admin" },
        };

        return new JsonResult(responseProperties) { StatusCode = 200 };
    }

In my Azure AD B2C I have a custom attribute called "Role".

But in debug mode I saw that for all the custom attributes extension_ is set as prefix...

So by adding this to responseProperties it seems to be working.

enter image description here

enter image description here

Steffen
  • 140
  • 1
  • 5
0

I solved the problem a long time ago, but maybe my experience will help someone. First part of the question:

"The body of the JsonElement contains no custom attributes. Inside I see body.ValueKind = Undefined"

What was the problem. To support HTTP Patch requests, I added the NewtonsoftJson package and configuration to Startup:

services.AddControllers().AddNewtonsoftJson(x => 
{
   x.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});

But I didn’t consider that AddNewtonsoftJson replaces the System.Text.Json-based input and output formatters used for formatting all JSON content. Because of this I was getting the above problem. Solution from Microsoft documentation

The solution to the second part of the question with a custom claims was provided by Steffen. You just need to add extension_ prefix to claim.