John Mc John Mc - 4 months ago 32
ASP.NET (C#) Question

asp.net identity 2.0 Facebook public_profile not returning fields

I'm trying to use Facebook as an external login provider with Asp.net Identity 2.0. I have my Facebook Authentication options in Startup.Auth configured like:

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


The email scope works because I can see my email address in the claims returned. However, none of the public_profile fields listed here are returned. Does anyone know why this might be the case?

DSR DSR
Answer

You can't directly get Facebook profile information from claims added through facebookOptions scope. You have to add scopes as you did in your sample and use FacebookClient.

Try using 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

Hope this helps.