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

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

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

问题

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

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

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

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

  1. {
  2. "users": [
  3. {
  4. "displayName": "Sharad Dutta",
  5. "givenName": "",
  6. "surname": "",
  7. "extension_user_type": "user",
  8. "identities": [
  9. {
  10. "signInType": "emailAddress",
  11. "issuerAssignedId": "00bonded007@gmail.com"
  12. }
  13. ],
  14. "extension_timezone": "VET",
  15. "extension_locale": "en-GB",
  16. "extension_tenant": "EG12345"
  17. },
  18. {
  19. "displayName": "Wayne Rooney",
  20. "givenName": "Wayne",
  21. "surname": "Rooney",
  22. "extension_user_type": "user",
  23. "identities": [
  24. {
  25. "signInType": "userName",
  26. "issuerAssignedId": "00bonded007"
  27. }
  28. ],
  29. "extension_timezone": "VET",
  30. "extension_locale": "en-GB",
  31. "extension_tenant": "EG12345"
  32. }
  33. ]
  34. }

当我创建用户时,只要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

  1. 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.

  1. User buildUserRequest = graphClient.users()
  2. .buildRequest()
  3. .post(createNewUser);
  4. 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 :

  1. {
  2. "users": [
  3. {
  4. "displayName": "Sharad Dutta",
  5. "givenName": "",
  6. "surname": "",
  7. "extension_user_type": "user",
  8. "identities": [
  9. {
  10. "signInType": "emailAddress",
  11. "issuerAssignedId": "00bonded007@gmail.com"
  12. }
  13. ],
  14. "extension_timezone": "VET",
  15. "extension_locale": "en-GB",
  16. "extension_tenant": "EG12345"
  17. },
  18. {
  19. "displayName": "Wayne Rooney",
  20. "givenName": "Wayne",
  21. "surname": "Rooney",
  22. "extension_user_type": "user",
  23. "identities": [
  24. {
  25. "signInType": "userName",
  26. "issuerAssignedId": "00bonded007"
  27. }
  28. ],
  29. "extension_timezone": "VET",
  30. "extension_locale": "en-GB",
  31. "extension_tenant": "EG12345"
  32. }
  33. ]
  34. }

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

使用以下代码:

  1. LinkedList<QueryOption> requestOptions = new LinkedList<>();
  2. requestOptions.add(new QueryOption("$filter", "identities/any(c:c/issuerAssignedId eq '00bonded007@gmail.com' and c/issuer eq 'contoso.onmicrosoft.com')"));
  3. IUserCollectionPage Users = graphClient.users()
  4. .buildRequest(requestOptions)
  5. .get();

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

英文:

Use this:

  1. LinkedList&lt;QueryOption&gt; requestOptions = new LinkedList&lt;&gt;();
  2. 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;));
  3. IUserCollectionPage Users = graphClient.users()
  4. .buildRequest(requestOptions)
  5. .get();

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

答案2

得分: 1

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

  1. User buildUserRequest = null;
  2. try {
  3. buildUserRequest = graphClient.users()
  4. .buildRequest()
  5. .post(createNewUser);
  6. } catch (GraphServiceException gse) {
  7. LOG.warn("Skipping! userPrincipalName already exists | signInType : " + signInType + " & issuerAssignedId " + issuerAssignedId);
  8. exitFlag = true;
  9. LOG.info("Setting exitFlag for existing user : " + exitFlag);
  10. }
  11. if (exitFlag == true) {
  12. return null;
  13. } else {
  14. return buildUserRequest.id;
  15. }

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

英文:

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

  1. User buildUserRequest = null;
  2. try {
  3. buildUserRequest = graphClient.users()
  4. .buildRequest()
  5. .post(createNewUser);
  6. } catch (GraphServiceException gse) {
  7. LOG.warn(&quot;Skipping! userPrincipalName already exists | signInType : &quot; + signInType + &quot; &amp; issuerAssignedId &quot; + issuerAssignedId);
  8. exitFlag = true;
  9. LOG.info(&quot;Setting exitFlag for existing user : &quot; + exitFlag);
  10. }
  11. if (exitFlag == true) {
  12. return null;
  13. } else {
  14. return buildUserRequest.id;
  15. }

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:

确定