Reputation: 1962
I am using Facebook SDK to get email of user. I write this code in Startup.Auth.css class :
var x = new FacebookAuthenticationOptions();
x.Scope.Add("email");
x.AppId = "421656951358309";
x.AppSecret = "5a2e5ccf163fe6918b6d6fdc8dda5184";
x.Provider = new FacebookAuthenticationProvider()
{
OnAuthenticated = async context =>
{
context.Identity.AddClaim(new System.Security.Claims.Claim("FacebookAccessToken", context.AccessToken));
}
};
x.SignInAsAuthenticationType = DefaultAuthenticationTypes.ExternalCookie;
app.UseFacebookAuthentication(x);
And in controller, I use two method to get email but both methods fail. Here is the methods :
Method 1 : By default, after executing the first line the variable info has email property. But it always return null.
var info = await AuthenticationManager.GetExternalLoginInfoAsync();
string email = info.Email;
Method 2 : I use the following code, it also return null.
var fb = new FacebookClient(access_token.Value);
dynamic me = fb.Get("email");
string email = me.email;
Did I miss something ? or there is another method that I can use. I can get others information such as username, but for email I can't.
Upvotes: 3
Views: 707
Reputation: 58
Method 2 should be work with facebook app before version 2.4, but for version 2.4 and I guess it is the version of your app you should edit :
dynamic me = fb.Get("/me?fields=email");
Upvotes: 2
Reputation: 76
try to modify Method 2 as follow :
var fb = new FacebookClient(access_token.Value);
dynamic me = fb.Get("/me?fields=email");
string email = me.email;
Upvotes: 6