在 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

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

问题描述

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

app.UseFacebookAuthentication(new FacebookAuthenticationOptions{AppId = "XXX",AppSecret = "XXX",范围 = {电子邮件"}});

但是,任何只能面向 v2.4(7 月 8 日发布)的应用不再将电子邮件地址返回给 ExternalLoginCallback.

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

声明性字段

要尝试提高移动网络的性能,v2.4 中的节点和边缘要求您明确请求GET 请求所需的字段.例如,GET/v2.4/me/feed 默认不再包含赞和评论,但是GET/v2.4/me/feed?fields=comments,likes 将返回数据.更多详情请参阅有关如何请求特定字段的文档.

我现在如何访问此电子邮件地址?

解决方案

为了解决这个问题,我必须安装 适用于 .NET 的 Facebook SDK 来自 nuget 并单独查询电子邮件地址.

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

var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();如果(登录信息 == 空){return RedirectToAction("登录");}//添加以下几行if (loginInfo.Login.LoginProvider == "Facebook"){var 身份 = AuthenticationManager.GetExternalIdentity(DefaultAuthenticationTypes.ExternalCookie);var access_token = identity.FindFirstValue("FacebookAccessToken");var fb = new FacebookClient(access_token);动态 myInfo = fb.Get("/me?fields=email");//指定电子邮件字段loginInfo.Email = myInfo.email;}

为了获得 FacebookAccessToken 我扩展了 ConfigureAuth;

app.UseFacebookAuthentication(new FacebookAuthenticationOptions{AppId = "XXX",AppSecret = "XXX",范围 = { "email" },提供者 = 新的 FacebookAuthenticationProvider{OnAuthenticated = 上下文 =>{context.Identity.AddClaim(new System.Security.Claims.Claim("FacebookAccessToken", context.AccessToken));返回 Task.FromResult(true);}}});

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

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

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

Declarative Fields

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?

解决方案

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

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

And to get the FacebookAccessToken I extended 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天全站免登陆