Home > Net >  Reading Azure B2C custom attributes with Graph API
Reading Azure B2C custom attributes with Graph API

Time:09-17

I have created an Azure B2C custom attribute called IsAdmin on the portal, added it to a Sign In / Sign Up user flow, and then using the Graph API, successfully created a new user with IsAdmin = true. If I then sign in using that new user I can see IsAdmin returned in the token as a claim. So far so good.

However I can't seem to see that custom attribute when querying via Graph API, nor can I search for it.

    var user = await graphClient.Users["{GUID HERE}"]
        .Request()
        .GetResponseAsync();

The user is returned, but the custom attribute is not present.

    var results = await graphClient.Users
        .Request()
        .Filter("extension_anotherguid_IsAdmin eq true")
        .GetAsync();

Returns no results.

Does anyone have any idea?

CodePudding user response:

Extensions are not returned by default. You need specify the extension in Select

var user = await graphClient.Users["{GUID HERE}"]
        .Request()
        .Select("extension_anotherguid_IsAdmin")
        .GetResponseAsync();

The value should be available through AdditionalData.

var extValue = user.AdditionalData["extension_anotherguid_IsAdmin"];

Resources:

enter image description here

Take the app id of this app registration, remove the dashes in the id and then use it like below :

import requests

# if your app registration b2c extensions app id id is aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee :
b2c-extensions-app-id-without-dashes="aaaaaaaabbbbccccddddeeeeeeeeeeee" 
url = f"https://graph.microsoft.com/v1.0/users/?$select=extension_ {b2c-extensions-app-id-without-dashes}_IsAdmin"
        headers = {
        'Content-type': 'application/json',
        'Authorization': 'Bearer '   msgraph_token
        }
r = requests.request("GET", url, headers=headers) 
  • Related