【问题标题】:How to retrieve facebook user profile picture within asp.net core MVC RC2如何在 asp.net core MVC RC2 中检索 facebook 用户个人资料图片
【发布时间】:2016-06-19 05:36:52
【问题描述】:

使用 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 集合中的图片数据,但我看不到任何与图片相关的声明。

这是检索 Facebook 用户个人资料图片的正确方法还是我遗漏了什么?

【问题讨论】:

    标签: asp.net-core asp.net-core-mvc facebook-authentication


    【解决方案1】:

    使用第 3 方身份验证,您只能获取有关用户身份的信息(用户身份验证)。

    使用 Facebook 身份验证,成功登录后,您会收到 Facebook API 的访问令牌和一组用户数据(声明)仅来自 UserInformationEndpoint (https://graph.facebook.com/v2.6/me)。

    之后,如果您想获取任何其他用户特定信息(如您的情况下的用户图片 - Facebook Graph API. User Picture),则应调用 Facebook API 方法。

    如果您对 Facebook 登录的 ASP.Core 实现已填充的声明感兴趣,请查看 FacebookHandler 类中的 CreateTicketAsync 方法 - 此处创建声明集合。

    【讨论】:

    • 感谢 Set 澄清声明主体集合仅包含来自 UserInformationEndpoint 的数据。
    • 你能给我举个例子来说明如何调用 facebook api 方法
    • 非常无益的答案...没有解释有关 Facebook auth 中间件的任何内容...
    【解决方案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()
    });
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-02-01
        • 2012-05-28
        • 1970-01-01
        • 2015-05-21
        • 1970-01-01
        • 2014-01-09
        • 1970-01-01
        相关资源
        最近更新 更多