Stephen H
Stephen H

Reputation: 138

Reading Azure B2C custom attributes with Graph API

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?

Upvotes: 3

Views: 1614

Answers (2)

Will
Will

Reputation: 2410

When storing custom attribute in a B2C tenant, a microsoft's managed app registration is created :

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) 

Upvotes: 3

user2250152
user2250152

Reputation: 20595

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:

Extensibility

Upvotes: 5

Related Questions