简体   繁体   中英

How to get profile picture with Microsoft.Graph SDK or User.Identity

I have implemented authentication successfully with the quickstart project in Microsoft Azure, but now I'm stuck with getting the profile picture of a logged-in user.

I have tried using Microsoft.Graph SDK, but the Photo keep getting null

                IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
                    .Create(ClientId)
                    .WithTenantId(TenantId)
                    .WithClientSecret(ClientSecret)
                    .Build();

                AuthorizationCodeProvider authProvider = new AuthorizationCodeProvider(confidentialClientApplication);

                // Create an authentication provider.
                ClientCredentialProvider authenticationProvider = new ClientCredentialProvider(confidentialClientApplication);
                // Configure GraphServiceClient with provider.
                GraphServiceClient graphServiceClient = new GraphServiceClient(authenticationProvider);
                var users = await graphServiceClient.Users.Request().GetAsync();

I also tried getting a specific user with id and .Select("Photo") but the result is the same

var temp = await graphServiceClient.Users[user_id]
                    .Request()
                    .Select("Photo")
                    .GetAsync();

Maybe my implementation was wrong, any help or suggestions will be appreciated.

To get a photo as a stream you can use this code

using (var photoStream = await graphServiceClient.Users[user_id].Photo.Content
                               .Request()
                               .GetAsync())
{
    // your code       
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM