Azure AD B2C - 如何以编程方式添加自定义属性(扩展属性)
问题描述:
为了让Azure AD B2C用户拥有一些额外的数据我想为User对象(或扩展属性,即I假设同样的事情)。所以我发现这个documentation。这是为用户添加自定义属性的正确方法吗?Azure AD B2C - 如何以编程方式添加自定义属性(扩展属性)
答
添加属性的正确方法是通过'portal.azure.com'管理门户。
您还需要通过策略创建用户,然后才能为所有用户实际使用这些属性。
要考虑的另一件事是,扩展名在每个环境中都会有所不同,因此您需要一些自定义逻辑来解析从GraphApi获得的用户JSON,并检查确定该属性以你给这个属性的名字。
实施例:
//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;
}
答
您使用Graph API期望的应用程序对象上创建extensionProperty。
样品JSON请求:
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"
]
}
如果操作成功,它会返回一个完全合格的扩展属性的名称沿HTTP 201 Created状态码,它可以用于写入值目标类型。 参考:azure-ad-graph-api-directory-schema-extensions
为什么我不能以编程方式添加自定义属性?我有一个定义MyOwnUser的库,对于那个用户我需要自定义属性。为什么我会每次为不同的目录在门户中添加这些内容? – alvipeo
@alvipeo这是一个很好的问题!我自己曾问了很多次。同样令人沮丧的是,我必须手动创建具有这些属性值的帐户,才能使用这些帐户。如果我在编程创建方面死心塌地,我可能会更多地关注Azure PowerShell的B2C模块;我从来没有这样做,但也许你可以创建一个脚本来设置每个环境。我还没有使用定制策略,但我理解它们是更可配置的(xml和东西),虽然它需要更多的初始努力。 – Pytry
我刚刚在代码中创建了一个创建扩展属性的类。需要完成设置/阅读用户的价值 – alvipeo