Umang
Umang

Reputation: 71

How to get profile pictures of all Azure AD b2c users using Graph API

I want to get profile picture of all b2c users.

I have tried to get profile picture of logged in user and its working, but I didn't found way to get profile picture of other b2c users.

            using (HttpClient client = new HttpClient())
            {
                using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "https://graph.microsoft.com/v1.0/me/photo/$value"))
                {
                    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "<token>");
                    var response = await client.SendAsync(request);
                }
            }

I have tried to get all b2c user profiles by using following code, and able to get all user profiles data but photos are not available in that.

       var result = await graphClient.Users
            .Request()
            .GetAsync();

        List<Microsoft.Graph.User> users = (List<Microsoft.Graph.User>)result.CurrentPage;

Please help me for this !!!

Thank you in advance...

Upvotes: 0

Views: 1855

Answers (3)

Sruthi J
Sruthi J

Reputation: 1602

In Microsoft Graph, the photo is handled by the Exchange service, except there are certain cases where this won't work. B2C appears to be one of those cases because B2C tenants don't get licensed for Exchange. The alternative, for now, is to use the Microsoft Graph /beta endpoint. That should work, as it falls back to AAD to get/set photos.

Upvotes: 0

Homungus
Homungus

Reputation: 1114

As you already have the profile data of all the users, just iterate over them and use the id of each user:

var photoStream = await graphClient.Users[userId].Photo.Content.Request().GetAsync();

For testing you can use Postman, where the call would be like this:

https://graph.microsoft.com/v1.0/users/{{UserId}}/photo/$value

Check out the link to the microsoft postman collection

Upvotes: 1

Carl Zhao
Carl Zhao

Reputation: 9511

Unfortunately, for Azure b2c users, it is currently not possible to call the Graph API, so you cannot get the profile pictures of all Azure AD b2c users by calling the Graph API: https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/issues/526

Upvotes: 0

Related Questions