I'm using OWIN to authenticate users in a MVC website. The login works, but I have no success trying to get the user Email. The only two values I'm getting are user name and id.
As documented, and samples, I've tried using this code in Startup.Auth.cs:
var opt = new FacebookAuthenticationOptions
{
AppId = "...",
AppSecret = "...",
};
opt.Scope.Add("email");
app.UseFacebookAuthentication(opt);
I've also tried providing the method OnAuthenticated
as parameter, and if I debug it, the only properties gotten from facebook identity are the user name and id as stated before. No other data is present:
const string XmlSchemaString = "http://www.w3.org/2001/XMLSchema#string";
var opt = new Microsoft.Owin.Security.Facebook.FacebookAuthenticationOptions()
{
AppId = "...",
AppSecret = "...",
SignInAsAuthenticationType = DefaultAuthenticationTypes.ExternalCookie,
Provider = new Microsoft.Owin.Security.Facebook.FacebookAuthenticationProvider()
{
OnAuthenticated = (context) =>
{
// context.User only have two keys: name and id, email is not there...
context.Identity.AddClaim(new System.Security.Claims.Claim("urn:facebook:access_token", context.AccessToken, XmlSchemaString, "Facebook"));
foreach (var x in context.User)
{
var claimType = string.Format("urn:facebook:{0}", x.Key);
string claimValue = x.Value.ToString();
if (!context.Identity.HasClaim(claimType, claimValue))
context.Identity.AddClaim(new System.Security.Claims.Claim(claimType, claimValue, XmlSchemaString, "Facebook"));
}
return Task.FromResult(0);
}
}
};
opt.Scope.Add("email");
app.UseFacebookAuthentication(opt);
Tried to log in with 3 different facebook accounts, same result.
I have tried samples from here, and also similar questions from SO (where users stated that worked for them) without success.
UPDATE:
As pointed out by CBroe, this problem could be related to a change in latest API version 2.4
I'm looking for an answer which makes use of Microsoft.Owin.Security.Facebook with the current latest version of facebook api, where I could successfully retrieve the email of authenticated user, and make it available in the context identity claims.