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

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

问题描述

使用Facebook API v2.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

However, any app that can only target v2.4 (released 8 July) no longer returns the email address to the ExternalLoginCallback.

我认为这可能与列出的v2.4更改有关href =https://developers.facebook.com/docs/apps/changelog#v2_4_changes> here ;

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


声明性字段

为了提高移动网络的性能,
v2.4中的节点和边缘要求您明确请求
您需要的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.

现在如何访问此电子邮件地址? / p>

How can I access this email address now?

推荐答案

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

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

ExternalLoginCallback 方法,我添加了一个条件来填充Facebook Graph 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);
        }
    }
});

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

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