从ASP.NET MVC 5中的Facebook v2.4 API访问OAuth ExternalLoginCallback中的电子邮件地址

使用Facebook API的v2.3,如果设置了以下内容,用户的电子邮件地址将在callback到ExternalLoginCallback返回;

 app.UseFacebookAuthentication(new FacebookAuthenticationOptions { AppId = "XXX", AppSecret = "XXX", Scope = { "email" } }); 

但是,只能定位到v2.4(7月8日发布)的任何应用程序不会将电子邮件地址返回给ExternalLoginCallback

我认为这可能与此处列出的v2.4更改有关;

声明性字段

为了提高移动networking的性能,v2.4中的节点和边缘要求您明确地请求您的GET请求所需的字段。 例如, GET /v2.4/me/feed默认不再包含喜欢和评论,但GET /v2.4/me/feed?fields=comments,likes将返回数据。 有关更多详细信息,请参阅有关如何请求特定字段的文档。

我现在怎样才能访问这个电子邮件地址?

为了解决这个问题,我必须从nuget安装.NET SDK for .NET ,并分别查询电子邮件地址。

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

在MVC 6(Asp.net Core 1.0)中,通过在startup.cs中configurationFacebookAuthentication,如下所示:

  app.UseFacebookAuthentication(options => { options.AppId = Configuration["Authentication:Facebook:AppId"]; options.AppSecret = Configuration["Authentication:Facebook:AppSecret"]; options.Scope.Add("email"); options.UserInformationEndpoint = "https://graph.facebook.com/v2.4/me?fields=id,name,email,first_name,last_name"; }); 

我可以得到电子邮件。 即:

  var info = await _signInManager.GetExternalLoginInfoAsync(); var email = info.ExternalPrincipal.FindFirstValue(ClaimTypes.Email);