How to get an external login profile image from a Microsoft account in asp.net core

How to get profile picture from Microsoft account using Microsoft.AspNetCore.Authentication.Facebook library? I tried using Claims, but they do not have a profile image value ... I also tried to find in the invoice source control by checking the image URL, but I noticed that the URL is made up of some parameters that I cannot get with claims so i cant create url as i can using facebook ... Can someone help me?

+4
source share
1 answer

You can get the profile image from Microsoft accounts using Microsoft Graph: https://developer.microsoft.com/en-us/graph/quick-start

Specific instructions on how to request a profile picture: https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/profilephoto_get

If you have a quick start (select asp.net, click on “Get Application ID and Secret” and download the sample code), it’s easy to get the data as follows:

GraphServiceClient graphClient = SDKHelper.GetAuthenticatedClient();

var photoStream = await graphService.GetCurrentUserPhotoStreamAsync(graphClient);

EDIT: Sorry, I forgot the main part of asp.net (it doesn't look like Microsoft.Identity.Client is available for the asp.net core).

ExternalLoginCallback ExternalLoginInfo, var info = await _signInManager.GetExternalLoginInfoAsync(); SaveTokens true ( ):

services.AddAuthentication()
    .AddMicrosoftAccount(options =>
    {
        options.ClientId = Configuration["ExternalProviders:Microsoft:ClientId"];
        options.ClientSecret = Configuration["ExternalProviders:Microsoft:ClientSecret"];
        options.SaveTokens = true;
        ...

http- - - :

var httpClient = new HttpClient();
httpClient.SetBearerToken(info.AuthenticationTokens.Where(t => t.Name.Equals("access_token")).First().Value);
var pictureResult = httpClient.GetAsync("https://graph.microsoft.com/v1.0/me/photo/$value").Result;
0

Source: https://habr.com/ru/post/1677683/


All Articles