2014-09-18 3 views
1

Asp.net Identity 2.0으로 외부 로그인 공급자로 Facebook을 사용하려고합니다. Startup.Auth의 Facebook 인증 옵션은 다음과 같이 구성됩니다.asp.net identity 2.0 Facebook public_profile이 필드를 반환하지 않습니다.

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 중 하나도 반환되지 않습니다. 아무도 이것이 왜 그런지 알 수 있습니까?

답변

2

facebookOptions 범위를 통해 추가 된 소유권 주장에서 Facebook 프로필 정보를 직접 가져올 수 없습니다. 샘플에서했던 것처럼 스코프를 추가하고 FacebookClient를 사용해야합니다.

Refer to this article

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; } 
    } 
희망이 도움을 사용하여

보십시오.

당신이 그것을 얻을 수
0

참조

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())); 
      } 
     }; 
관련 문제