【问题标题】:Azure AD B2C - how to add custom attribute programmatically (extension property)Azure AD B2C - 如何以编程方式添加自定义属性(扩展属性)
【发布时间】:2017-10-13 19:11:34
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    您使用 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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 为什么我不能以编程方式添加自定义属性?我有一个定义 MyOwnUser 的库,对于该用户,我需要自定义属性。为什么我每次都在门户中为不同的目录添加这些?
      • @alvipeo 这是一个很好的问题!我自己已经问过很多次了。同样令人沮丧的是,我必须在这些属性值可用之前手动创建一个帐户。如果我对程序化创建一无所知,我可能会更多地研究 azure powershell 的 B2C 模块;我从来没有做过,但也许你可以创建一个脚本来设置每个环境。我还没有使用自定义策略,但我知道它们更易于配置(xml 和其他东西),尽管它需要更多的初始努力。
      • 我刚刚创建了一个在代码中创建扩展属性的类。需要完成从用户那里设置/读取其值
      • 你能分享你为此编写的代码吗?另外,您使用的是哪个 api 版本?此外,如果您想查看我为更新用户和获取自定义属性所做的工作,请看这里:github.com/Xitikit/xitikit-blue/blob/master/blue-kit/graph-api/… 它基于我在生产中使用的内容,但此版本仍在进行中。出于许可和法律原因,我不得不更改一些内容,以便每个人都可以使用它。
      • 我现在已经有了整个库和一堆测试。差不多好了。当它全是绿色的时候会分享一些。
      猜你喜欢
      • 2019-04-13
      • 1970-01-01
      • 1970-01-01
      • 2019-12-27
      • 1970-01-01
      • 1970-01-01
      • 2019-10-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多