2
votes

I am using Azure B2C, followed by the article

https://docs.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?

2
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
Got error "StatusCode: 400, ReasonPhrase: 'Bad Request'," How to get a user with particular username ?Jinesh
/users/[email protected] :)juunas
Thanks, but I tried var payload = await client.GetAsync("graph.windows.net /************.onmicrosoft.com/users?api-version=1.6/username.onmicrosoft.com"); got error "StatusCode: 400, ReasonPhrase: 'Bad Request'," Is any mistake?Jinesh
I meant graph.windows.net /************.onmicrosoft.com/users/username.onmicrosoft.com?api-version=1.6juunas

2 Answers

4
votes

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 '[email protected]')&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`
0
votes

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();
    }