Azure AD B2C - 如何以编程方式添加自定义属性(扩展属性)

时间:2017-10-11 19:49:36

标签: c# azure-ad-b2c azure-ad-graph-api

为了为我的Azure AD B2C用户提供一些额外的数据,我想为User对象(或扩展属性创建一个新的自定义属性,我想这是同样的事情)。所以我发现了这个 documentation 。这是为用户添加自定义属性的正确方法吗?

2 个答案:

答案 0 :(得分:1)

使用Graph API在所需的Application对象上创建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

答案 1 :(得分:0)

添加属性的正确方法是通过“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;
}
相关问题