How to retrieve facebook user profile picture within asp.net core MVC RC2

随声附和 提交于 2019-12-04 13:40:46

Using 3rd Party Authentication, you are only get information about who user is (user authentication).

Using Facebook Authentication, after successful login you receive an access token to Facebook API and a collection of user data (claims) only from UserInformationEndpoint (https://graph.facebook.com/v2.6/me).

After that, you should call Facebook API methods if you want to get any other user specific information (like user picture in your case - Facebook Graph API. User Picture).

If you are interesting about claims that ASP.Core implementation of Facebook login is filled, looks into CreateTicketAsync method in FacebookHandler class - here the collection of claims is created.

For anyone using Microsoft' OWIN Facebook Authentication extensions. You can extend the default FacebookAuthenticationProvider to obtain any requested (and granted) claims to the current identity. Then register that customer provider in the middleware. A simple custom provider would look like this...

public class CustomFacebookOAuthProvider: FacebookAuthenticationProvider
    {
        public override Task Authenticated(FacebookAuthenticatedContext context)
        {
            if (context.User.IsNotNull() && context.Id.IsNotNullOrEmpty())
            {
                //ADD THE CLAIMS YOU WANT YOUR APP TO CONSUME
                context.Identity.AddClaim(new Claim(Constants.ClaimsTypes.PhotoUrl
                    , "https://graph.facebook.com/{0}/picture?height={1}&width={1}".With(context.Id, Constants.UI.DEFAUL_PROFILE_PICTURE_DIMENS)));
            }

            return base.Authenticated(context);
        }
    }

The implementation is simple, we only need to override the Authenticated method, which will be called once the OAuth provider (Facebook in this example) has authenticated the user and returned the user details (translate this to a call to the UserInfo endpoint).

The context.User is a NewtonSoft.Json.JObject which can be desrialized into your own class...

var user = context.User.ToObject<FacebookOAuthUser>();

Just make sure you create the FacebookOAuthUser class with the required properties.

Once you have that provider in place, all you need to do register it...

app.UseFacebookAuthentication(new FacebookAuthenticationOptions
{
    AuthenticationType = "Facebook",
    Caption = "Sign-in with Facebook",
    SignInAsAuthenticationType = signInAsType,
    AppId = ApplicationSettings.FacebookAppID,
    AppSecret = ApplicationSettings.FacebookAppSecret,
    Provider = new CustomFacebookOAuthProvider()
});

To get profile picture from Facebook, you need to configure Facebook options and subscribe at OnCreatingTicket event from OAuth.

services.AddAuthentication().AddFacebook("Facebook", options =>
                {

                    options.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;
                    options.ClientId = "ClientId";
                    options.ClientSecret = "ClientSecret";
                    options.Fields.Add("picture"); // <- Add field picture
                    options.Events = new OAuthEvents
                    {
                        OnCreatingTicket = context =>
                        {
                            var identity = (ClaimsIdentity)context.Principal.Identity;
                            var profileImg = context.User["picture"]["data"].Value<string>("url");
                            identity.AddClaim(new Claim(JwtClaimTypes.Picture, profileImg));
                            return Task.CompletedTask;
                        }
                    };
                });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!