如何在ASP.NET Core MVC RC2中检索Facebook用户个人资料图片

4
使用ASP.NET Core MVC RC2,我正在尝试检索Facebook用户的个人资料图片。为此,我在Startup类的Configure方法中有以下几行代码。
            app.UseFacebookAuthentication(new FacebookOptions()
        {
            AppId = Configuration["Authentication:Facebook:AppId"],
            AppSecret = Configuration["Authentication:Facebook:AppSecret"],
            Scope = { "user_birthday" },
            Fields = { "birthday", "picture" },
        });

在 AccountControllers 的 ExternalLoginCallback 方法中,我期望可以通过 info.Principal 访问到 ClaimsPrincipal 集合中的图片数据,但是我发现没有任何与图片相关的 claim。
这是获取 Facebook 用户个人资料图片的正确方法吗?还是我漏掉了什么?
3个回答

2
使用第三方认证,你只会得到关于用户身份的信息(用户认证)。
使用Facebook认证,在成功登录后,你会收到一个访问Facebook API的访问令牌和一组用户数据(声明),只能从UserInformationEndpointhttps://graph.facebook.com/v2.6/me)获得。
之后,如果你想获取任何其他特定于用户的信息(例如用户图片),则应调用Facebook API方法(请参阅Facebook Graph API. User Picture)。
如果你对ASP.Core实现的Facebook登录填充的声明感兴趣,请查看FacebookHandler类中的CreateTicketAsync方法 - 这里创建了声明集合。

感谢Set澄清,声明主体集合仅包含来自UserInformationEndpoint的数据。 - Coni
你能给我一个示例,展示如何调用Facebook API方法吗? - Coni
请查看以下链接:https://dev59.com/Vmgu5IYBdhLWcg3wRlL0 - Set
非常没有帮助的答案... 没有解释有关Facebook身份验证中间件的任何内容... - Ahmad

2

如果您正在使用Microsoft的OWIN Facebook身份验证扩展,您可以扩展默认的FacebookAuthenticationProvider以获取当前身份的任何请求(和授予)声明。然后在中间件中注册该自定义提供程序。一个简单的自定义提供程序应该像这样...

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);
        }
    }

实现很简单,我们只需要重写 Authenticated 方法,在 OAuth 提供程序(例如 Facebook)验证用户并返回用户详细信息后调用该方法(将其转换为对 UserInfo 端点的调用)。 context.User 是一个 NewtonSoft.Json.JObject,可以反序列化为您自己的类...
var user = context.User.ToObject<FacebookOAuthUser>();

请确保您创建了具有所需属性的FacebookOAuthUser类。

一旦您已经设置好这个提供程序,您只需要注册它即可...

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

1
为了从Facebook获取个人资料图片,您需要配置Facebook选项并在OAuth的OnCreatingTicket事件中订阅。
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;
                        }
                    };
                });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接