azureazure-ad-b2c

AADB2C: Unable to assign a custom attribute in the "BeforeCreatingUser" API Connector


I'm using the Sign-in Sign-up User Flow in B2C. I have defined a custom user attribute MyAttribute, and I want to assign this attribute value in the BeforeCreatingUser event using an API connector.

I already use the BeforeCreatingUser event in an Azure Function API connector, where I successfully assign the displayName attribute. However, when assign my custom attribute extension_137c4e387a114659a8f721b24694843a_MyAttribute in same code, it does not get stored in B2C.

This is my Azure Function that is executed by the API Connector before a B2C user is created:

[Function("BeforeCreatingUser")]
public async Task<ObjectResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "BeforeCreatingUser")] HttpRequest req)
{
    
    /* deleted a lot for brevity */
 
    var someValue = "123456";
 
    // Preparing the response for B2C 

    var resultObj = new
    {
        version = "1.0.0",
        action = "Continue",
        displayName = $"{requestJson.givenName} {requestJson.surname}",
        extension_137c4e387a114659a8f721b24694843a_MyAttribute = someValue
    };

    return new OkObjectResult(resultObj);
}

The displayName attribute is successfully assigned for the new user, however the custom attribute MyAttribute is not assigned.

I have verified the AppId 137c4e387a114659a8f721b24694843a is correct (Application ID of the B2C tenant's b2c-extensions-app registered application.

Any idea what I'm doing wrong? Or, is it even possible to assign a custom attribute in the BeforeCreatingUser event?

Thanks.


Solution

  • If you want to write extension properties returned from an API connector to the directory then you need to make sure those attributes have been selected under User Attributes of the user flow.

    You can assign custom attributes to a user flow by: