【问题标题】:asp.net identity 2.0 Facebook public_profile not returning fieldsasp.net identity 2.0 Facebook public_profile 不返回字段
【发布时间】:2014-11-13 09:25:59
【问题描述】:

我正在尝试使用 Facebook 作为 Asp.net Identity 2.0 的外部登录提供程序。我在 Startup.Auth 中的 Facebook 身份验证选项配置如下:

var facebookOptions = new FacebookAuthenticationOptions()
        {
            AppId = ConfigurationManager.AppSettings[OneStepCloserTo.Web.Models.Constants.FacebookClientIdKey],
            AppSecret = ConfigurationManager.AppSettings[OneStepCloserTo.Web.Models.Constants.FacebookClientSecretKey]
        };

        facebookOptions.Scope.Add("email");
        facebookOptions.Scope.Add("user_friends");
        facebookOptions.Scope.Add("public_profile");
        facebookOptions.Scope.Add("user_hometown");

电子邮件范围有效,因为我可以在返回的声明中看到我的电子邮件地址。但是,不会返回任何 public_profile fields listed here。有谁知道为什么会这样?

【问题讨论】:

    标签: asp.net facebook-graph-api asp.net-identity


    【解决方案1】:

    您不能直接从通过 facebookOptions 范围添加的声明中获取 Facebook 个人资料信息。您必须像在示例中那样添加范围并使用 FacebookClient。

    尝试使用 FacebookClient

    [Authorize]
    public async Task<ActionResult> FacebookInfo()
    {
        var claimsforUser = await UserManager.GetClaimsAsync(User.Identity.GetUserId());
        var access_token = claimsforUser.FirstOrDefault(x => x.Type == "FacebookAccessToken").Value;
        var fb = new FacebookClient(access_token);
        dynamic myInfo = fb.Get("/me/friends");
        var friendsList = newList<FacebookViewModel>();
        foreach (dynamic friend in myInfo.data)
        {
            friendsList.Add(newFacebookViewModel()
               {
                   Name = friend.name,
                   ImageURL = @"https://graph.facebook.com/" + friend.id + "/picture?type=large"
               });
        }
    
        return View(friendsList);
    } 
    
    
      public class FacebookViewModel
      {
           [Required]
           [Display(Name = "Friend's name")]
           public string Name { get; set; }
           public string ImageURL { get; set; }
      }
    

    Refer to this article

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      你可以得到它,看

      var fbOptions = new FacebookAuthenticationOptions();
              fbOptions.AppId = ...
              fbOptions.AppSecret = ...
              fbOptions.Fields.Add("email"); 
              fbOptions.Fields.Add("first_name");
              fbOptions.Fields.Add("last_name"); 
              fbOptions.Scope.Add("public_profile");
              fbOptions.Scope.Add("email");
              fbOptions.Provider = new FacebookAuthenticationProvider()
              {
                  OnAuthenticated = async context =>
                  {
                      JToken value;
                      if (context.User.TryGetValue("first_name", out value))
                          context.Identity.AddClaim(new Claim("FacebookFirstName", value.ToString()));
                      if (context.User.TryGetValue("last_name", out value))
                          context.Identity.AddClaim(new Claim("FacebookLastName", value.ToString()));
                  }
              };
      

      【讨论】:

        猜你喜欢
        • 2014-12-25
        • 2016-07-12
        • 1970-01-01
        • 2018-06-03
        • 1970-01-01
        • 2014-12-14
        • 2019-07-21
        • 2019-02-11
        • 2015-02-09
        相关资源
        最近更新 更多