对于 Facebook API v2.3,如果设置了以下内容,用户的电子邮件地址将在回调中返回ExternalLoginCallback
;
app.UseFacebookAuthentication(new FacebookAuthenticationOptions
{
AppId = "XXX",
AppSecret = "XXX",
Scope = { "email" }
});
但是,任何只能定位 v2.4(7 月 8 日发布)的应用程序不再将电子邮件地址返回到ExternalLoginCallback
.
我认为这可能与列出的 v2.4 更改有关here https://developers.facebook.com/docs/apps/changelog#v2_4_changes;
声明字段
为了尝试提高移动网络的性能,
v2.4 中的节点和边要求您明确请求
GET 请求所需的字段。例如,GET
/v2.4/me/feed
默认情况下不再包含点赞和评论,但是GET /v2.4/me/feed?fields=comments,likes
将返回数据。了解更多
详细信息请参阅有关如何请求特定字段的文档。
我现在如何访问该电子邮件地址?
为了解决这个问题,我必须安装Facebook SDK for .NET https://www.nuget.org/packages/Facebook/从 nuget 并单独查询电子邮件地址。
In the ExternalLoginCallback
方法中,我添加了一个条件来从 Facebook Graph API 填充电子邮件地址;
var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
if (loginInfo == null)
{
return RedirectToAction("Login");
}
// added the following lines
if (loginInfo.Login.LoginProvider == "Facebook")
{
var identity = AuthenticationManager.GetExternalIdentity(DefaultAuthenticationTypes.ExternalCookie);
var access_token = identity.FindFirstValue("FacebookAccessToken");
var fb = new FacebookClient(access_token);
dynamic myInfo = fb.Get("/me?fields=email"); // specify the email field
loginInfo.Email = myInfo.email;
}
并得到FacebookAccessToken
我延长了ConfigureAuth
;
app.UseFacebookAuthentication(new FacebookAuthenticationOptions
{
AppId = "XXX",
AppSecret = "XXX",
Scope = { "email" },
Provider = new FacebookAuthenticationProvider
{
OnAuthenticated = context =>
{
context.Identity.AddClaim(new System.Security.Claims.Claim("FacebookAccessToken", context.AccessToken));
return Task.FromResult(true);
}
}
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)