Simple ASP.NET Core Facebook user interface

I am trying to get a user profile picture using the Facebook Authentication middleware in ASP.NET Core 1.0. I managed to add these configurations to make the user image available

app.UseFacebookAuthentication(new FacebookOptions()
        {
            AppId = Configuration["Authentication:Facebook:AppId"],
            AppSecret = Configuration["Authentication:Facebook:AppSecret"],
            Scope = { "public_profile" },
            Fields = { "picture" }
        });

and to retrieve data

var email = info.Principal.FindFirstValue(ClaimTypes.Email);

and

var userName = info.Principal.FindFirstValue(ClaimTypes.GivenName);

But how can I now get the image of the user, because for him there is no claim type ?

+4
source share
3 answers

As Set said in his answer, you can get the image using the Graph API , like this one https://graph.facebook.com/{user-id}/picture.

Code example:

var info = await _signInManager.GetExternalLoginInfoAsync();
var identifier = info.Principal.FindFirstValue(ClaimTypes.NameIdentifier); 
var picture = $"https://graph.facebook.com/{identifier}/picture";

, , info , info.LoginProvider - facebook.

+4

, (oauth) UserInfo , Fields = { "picture" }.

Facebook Graph API https://graph.facebook.com/v2.6/me UserInformation, ASP.NET Core Facebook Auth Middleware .

, API- Facebook picture , \me. , - . SO: facebook oauth,

, : https://graph.facebook.com/USERNAME/picture

+1

In my ASP.NET Core 2.2 project, I use this code:

services.AddAuthentication()
    .AddFacebook(options =>
    {
        options.AppId = Configuration["Authentication:Facebook:AppId"];
        options.AppSecret = Configuration["Authentication:Facebook:AppSecret"];
        options.Events.OnCreatingTicket = (context) =>
        {
            var picture = $"https://graph.facebook.com/{context.Principal.FindFirstValue(ClaimTypes.NameIdentifier)}/picture?type=large";
            context.Identity.AddClaim(new Claim("Picture", picture));
            return Task.CompletedTask;
        };
    });

And in the Controller, in the ExternalLoginCallback action, I get the value as follows:

var info = await _signInManager.GetExternalLoginInfoAsync();
var picture = info.Principal.FindFirstValue("Picture");
0
source

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


All Articles