Warm tip: This article is reproduced from serverfault.com, please click

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

发布于 2021-10-13 21:59:54

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?

Questioner
Александр Климук
Viewed
0
Steffen 2021-11-13 04:58:49

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