2

I am using Azure B2C, followed by the article

https://learn.microsoft.com/en-us/azure/active-directory-b2c/active-directory-b2c-devquickstarts-graph-dotnet

User is added successfully. But the issue is how to check the user exist or not with a user name, when I creating a new user?

Jinesh
  • 1,480
  • 2
  • 25
  • 52
  • Pretty sure you would get an error in that case? :) Best way in my opinion would be to handle that error rather than checking and then creating, introducing a race condition into the code. – juunas Sep 12 '18 at 06:39
  • Got error "StatusCode: 400, ReasonPhrase: 'Bad Request'," How to get a user with particular username ? – Jinesh Sep 12 '18 at 06:46
  • /users/first.last@company.com :) – juunas Sep 12 '18 at 06:54
  • Thanks, but I tried var payload = await client.GetAsync("https://graph.windows.net /************.onmicrosoft.com/users?api-version=1.6/username.onmicrosoft.com"); got error "StatusCode: 400, ReasonPhrase: 'Bad Request'," Is any mistake? – Jinesh Sep 12 '18 at 06:59
  • I meant `graph.windows.net /************.onmicrosoft.com/users/username.onmicrosoft.com?api-version=1.6` – juunas Sep 12 '18 at 10:37

2 Answers2

4

You can find users by their email address or their user name using the signInNames filter.

For an email address:

`GET https://graph.windows.net/myorganization/users?$filter=signInNames/any(x:x/value eq 'someone@somewhere.com')&api-version=1.6`

For a user name:

`https://graph.windows.net/myorganization/users?$filter=signInNames/any(x:x/value eq 'someone')&api-version=1.6`
Chris Padgett
  • 14,186
  • 1
  • 15
  • 28
  • Actually it return success, but when I tried which exist or not exist username, it return same. – Jinesh Sep 12 '18 at 07:19
  • In both cases, Azure AD Graph API returns 200 OK. If the sign-in name does exist, then a non-empty array, containing the matched user, is returned. If the sign-in name doesn't exist, then an empty array is returned. – Chris Padgett Sep 12 '18 at 07:28
  • I am getting "{"odata.error":{"code":"Request_InvalidDataContractVersion","message":{"lang":"en","value":"The specified api-version is invalid. The value must exactly match a supported version."}}}" .. Can you help please? @ChrisPadgett – DCodes Nov 26 '20 at 21:52
0

Programmatically, to check the user with the email address already exist. here is a solution using C# and Graph client library.

private async Task<User> CheckUserAlreadyExistAsync(string email, CancellationToken ct)
    {
        var filter = $"identities/any(c:c/issuerAssignedId eq '{email}' and c/issuer eq '{email}')";

        var request = _graphServiceClient.Users.Request()
            .Filter(filter)
            .Select(userSelectQuery)
            .Expand(e => e.AppRoleAssignments);

        var userCollectionPage = await request.GetAsync(ct).ConfigureAwait(false);

        return userCollectionPage.FirstOrDefault();
    }
Sagar Kulkarni
  • 636
  • 9
  • 24