Reputation: 4546
In order to have some extra data for my Azure AD B2C Users I want to create a new custom attribute for a User object (or extension property, which is I suppose the same thing). So I found this documentation. Is this a correct way of adding a custom attribute for a User?
Upvotes: 0
Views: 1493
Reputation: 196
You create extensionProperty on the desired Application object using Graph API.
Sample JSON request:
POST https://graph.windows.net/contoso.onmicrosoft.com/applications/269fc2f7-6420-4ea4-be90-9e1f93a87a64/extensionProperties?api-version=1.5 HTTP/1.1
Authorization: Bearer eyJ0eXAiOiJKV1Qi...r6Xh5KVA
Content-Type: application/json
Host: graph.windows.net
Content-Length: 104
{
"name": "skypeId",
"dataType": "String",
"targetObjects": [
"User"
]
}
If the operation was successful, it will return an HTTP 201 Created status code along with the fully-qualified extension property name, which can be used for writing values to the target type. Reference: azure-ad-graph-api-directory-schema-extensions
Upvotes: 1
Reputation: 6419
The correct way of adding attributes is through the 'portal.azure.com' administration portal.
You will also need to create a user through a policy before those attributes actually become available for all users.
The other thing to consider is that the extension name will be different in each environment, so you will need some custom logic to parse the users JSON that you get from the GraphApi, and check to be sure that the attribute ends with the name you gave that attribute.
Example:
//Java, sorry
private YourCustomGraphApiUser populateCustomProperty(JsonNode jsonUser) throws JsonProcessingException{
YourCustomGraphApiUser azureUser = mapper.treeToValue(jsonUser, YourCustomGraphApiUser.class);
String[] customAttributeNames = new String()["one","two"]; //Replace this with some values from some injected properties
for(String attributeName : customAttributeNames){
JsonNode customAttribute = jsonUser.get(attributeName);
if(customAttribute != null){
azureUser.set(attributeName, customAttribute.asText());//Assuming custom attributes are stored in a map-like structure in YourCustomGraphApiUser.
}
else{
throw new NotFoundException("Error getting the name for custom attribute "+attributeName, e);
// OR you could ignore and just log an error. Whatever.
}
}
return azureUser;
}
Upvotes: 0