使用”issuerAssignedId”作为对B2C目录的get()调用,以检查用户是否存在。[JAVA]

huangapple go评论85阅读模式
英文:

Use "issuerAssignedId" as a get() call to b2c directory to check if user exists. [JAVA]

问题

我正在尝试实现用于检查B2C目录中现有用户的get()调用,为此我正在使用以下代码:

User user = graphClient.users("422a535b-a60d-4e5f-833e-2789b32ca04f").buildRequest().get();

这将返回用户数据,但我之所以能够执行此操作,是因为我有在创建用户时发行的user_id

对于创建用户,我有一堆以JSON格式表示的用户数据,我正在尝试进行迁移,迁移工作正常进行,JSON看起来像这样:

{
  "users": [
    {
      "displayName": "Sharad Dutta",
      "givenName": "",
      "surname": "",
      "extension_user_type": "user",
      "identities": [
        {
          "signInType": "emailAddress",
          "issuerAssignedId": "00bonded007@gmail.com"
        }
      ],
      "extension_timezone": "VET",
      "extension_locale": "en-GB",
      "extension_tenant": "EG12345"
    },
    {
      "displayName": "Wayne Rooney",
      "givenName": "Wayne",
      "surname": "Rooney",
      "extension_user_type": "user",
      "identities": [
        {
          "signInType": "userName",
          "issuerAssignedId": "00bonded007"
        }
      ],
      "extension_timezone": "VET",
      "extension_locale": "en-GB",
      "extension_tenant": "EG12345"
    }
  ]
}

当我创建用户时,只要issuerAssignedId在活动目录中不存在,就可以正常工作。我想处理一种情况,即使用get()请求检查issuerAssignedId是否存在于B2C目录中,如果存在,则跳过该用户,以防止代码中断或由于异常而退出。

我可以使用try/catch来实现这一点,但我可以使用issuerAssignedId代替user_id来检查用户是否存在于B2C中吗?如果是的话,那么跳过该用户。

英文:

I am trying to implement get() call for checking the existing users in b2c directory, for that I am using

User user = graphClient.users("422a535b-a60d-4e5f-833e-2789b32ca04f").buildRequest().get();

This is returning the user data, however I am only able to do this because I have user_id which was issued at the time of creation of user.

User buildUserRequest = graphClient.users()
                                .buildRequest()
                                .post(createNewUser);

        return buildUserRequest.id;

I am having a bunch of users in JSON format that I am trying to migrate, the migration is working fine, JSON looking like this :

{
  "users": [
    {
      "displayName": "Sharad Dutta",
      "givenName": "",
      "surname": "",
      "extension_user_type": "user",
      "identities": [
        {
          "signInType": "emailAddress",
          "issuerAssignedId": "00bonded007@gmail.com"
        }
      ],
      "extension_timezone": "VET",
      "extension_locale": "en-GB",
      "extension_tenant": "EG12345"
    },
    {
      "displayName": "Wayne Rooney",
      "givenName": "Wayne",
      "surname": "Rooney",
      "extension_user_type": "user",
      "identities": [
        {
          "signInType": "userName",
          "issuerAssignedId": "00bonded007"
        }
      ],
      "extension_timezone": "VET",
      "extension_locale": "en-GB",
      "extension_tenant": "EG12345"
    }
  ]
}

When I am creating a user, as long as the issuerAssignedId is not existing in the active directory, it working fine. I want to handle a case where I am checking issuerAssignedid using get() request, if user exist in directory, skip it so that code will not break or will get out due to exception.

I can do this using try/catch but can I use issuerAssignedId in place of user_id to check if user is existing in the b2c, if yes, then skip for that user.

答案1

得分: 2

使用以下代码:

LinkedList<QueryOption> requestOptions = new LinkedList<>();
requestOptions.add(new QueryOption("$filter", "identities/any(c:c/issuerAssignedId eq '00bonded007@gmail.com' and c/issuer eq 'contoso.onmicrosoft.com')"));

IUserCollectionPage Users = graphClient.users()
        .buildRequest(requestOptions)
        .get();

请注意,你应该将 contoso.onmicrosoft.com 修改为你的 Azure B2C 租户。

英文:

Use this:

    LinkedList&lt;QueryOption&gt; requestOptions = new LinkedList&lt;&gt;();
    requestOptions.add(new QueryOption(&quot;$filter&quot;, &quot;identities/any(c:c/issuerAssignedId eq &#39;00bonded007@gmail.com&#39; and c/issuer eq &#39;contoso.onmicrosoft.com&#39;)&quot;));

    IUserCollectionPage Users = graphClient.users()
            .buildRequest(requestOptions)
            .get();

Please note that you should modify the contoso.onmicrosoft.com to your Azure B2C tenant.

答案2

得分: 1

回答时,当我们发送带有属性的post()请求,并返回:

User buildUserRequest = null;
try {
    buildUserRequest = graphClient.users()
            .buildRequest()
            .post(createNewUser);
          
} catch (GraphServiceException gse) {
    LOG.warn("Skipping! userPrincipalName already exists | signInType : " + signInType + " & issuerAssignedId " + issuerAssignedId);
    exitFlag = true;
    LOG.info("Setting exitFlag for existing user : " + exitFlag);
}

if (exitFlag == true) {
    return null;
} else {
    return buildUserRequest.id;
}

这将处理任何唯一的条件,不仅限于issuerAssignedId。

英文:

To answer, when we send a post() request with attributes, and return

User buildUserRequest = null;
        try {
            buildUserRequest = graphClient.users()
                    .buildRequest()
                    .post(createNewUser);
                  
        } catch (GraphServiceException gse) {
            LOG.warn(&quot;Skipping! userPrincipalName already exists | signInType : &quot; + signInType + &quot; &amp; issuerAssignedId &quot; + issuerAssignedId);
            exitFlag = true;
            LOG.info(&quot;Setting exitFlag for existing user : &quot; + exitFlag);
        }
        
        if (exitFlag == true) {
            return null;
        } else {
            return buildUserRequest.id;
        }

This will handle any unique criteria, not just issuerAssignedId.

huangapple
  • 本文由 发表于 2020年9月15日 15:57:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63897504.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定