在OAuth ExternalLoginCallback从Facebook API V2.4在ASP.NET MVC 5访问电子邮件地址 [英] Access email address in the OAuth ExternalLoginCallback from Facebook v2.4 API in ASP.NET MVC 5

查看:139
本文介绍了在OAuth ExternalLoginCallback从Facebook API V2.4在ASP.NET MVC 5访问电子邮件地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Facebook的API的2.3版,提供了以下设置,用户的电子邮件地址将在回调返回到 ExternalLoginCallback ;

With v2.3 of the Facebook API, provided the following was set, the users email address would be returned on the callback to ExternalLoginCallback;

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

但是,任何应用程序,只能针对V2.4(发布7月8日)不再返回的电子邮件地址到 ExternalLoginCallback

我觉得这有可能会涉及到V2.4变化此处上市;

I think this may possibly be related to the v2.4 changes as listed here;

声明字段

要尽量改善移动网络的性能,
  节点和边中的v2.4要求您明确要求
  场(S),你需要为你的GET请求。例如, GET
  /v2.4/me/feed
不再包括喜欢和默认的意见,但
   GET /v2.4/me/feed?fields=comments,likes 将返回的数据。欲了解更多
  详情请参看如何请求特定字段的文档。

To try to improve performance on mobile networks, Nodes and Edges in v2.4 requires that you explicitly request the field(s) you need for your GET requests. For example, GET /v2.4/me/feed no longer includes likes and comments by default, but GET /v2.4/me/feed?fields=comments,likes will return the data. For more details see the docs on how to request specific fields.

我现在如何才能访问该电子邮件地址?

How can I access this email address now?

推荐答案

要解决此,我不得不安装的Facebook SDK的。从NET的NuGet 并单独询问的电子邮件地址。

To resolve this I had to install the Facebook SDK for .NET from nuget and query the email address separately.

ExternalLoginCallback 方法,我添加了一个条件来填充来自Facebook的图形API的电子邮件地址;

In the ExternalLoginCallback method, I added a conditional to populate the email address from the 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);
        }
    }
});

这篇关于在OAuth ExternalLoginCallback从Facebook API V2.4在ASP.NET MVC 5访问电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆