加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 在OWIN登录上检索用户的Facebook电子邮件

发布时间:2020-12-16 03:30:25 所属栏目:asp.Net 来源:网络整理
导读:参见英文答案 Why new fb api 2.4 returns null email on MVC 5 with Identity and oauth 2?????????????????????????????????????6个 我正在学习如何使用VS 2015中的默认MVC模板使用OWIN外部auth.我启用了Facebook auth并添加了范围“email”,期望用户通过
参见英文答案 > Why new fb api 2.4 returns null email on MVC 5 with Identity and oauth 2?????????????????????????????????????6个
我正在学习如何使用VS 2015中的默认MVC模板使用OWIN外部auth.我启用了Facebook auth并添加了范围“email”,期望用户通过身份验证后返回用户的电子邮件(根据 this) .但是,上下文中没有电子邮件.用户JSON对象和context.Email也为空.

这是Startup.Auth.cs中的相关代码

var facebookOptions = new FacebookAuthenticationOptions
        {
            AppId = "XXXXXX",AppSecret = "XXXXXX",Provider = new FacebookAuthenticationProvider
            {
                OnAuthenticated =  context =>
                {
                    // Retrieve the OAuth access token to store for subsequent API calls
                    var accessToken = context.AccessToken;

                    // Retrieve the username
                    var facebookUserName = context.UserName;

                    // WHY IS IT EMPTY?
                    var facebookEmail = context.Email;

                    // You can even retrieve the full JSON-serialized user
                    var serializedUser = context.User;

                    return Task.FromResult(0);
                }
            }
        };

        facebookOptions.Scope.Add("email");

        app.UseFacebookAuthentication(facebookOptions);

什么缺少的想法?为什么没有退回Facebook电子邮件地址?

解决方法

事实证明,Facebook API v 2.4中存在重大变化,您必须指定要检索的字段.图形请求曾经是:

https://graph.facebook.com/v2.3/me?access_token=XXXXX

但是出于性能原因,从FB API v2.4开始,您还必须指定要在范围内检索的文件:

https://graph.facebook.com/v2.4/me?fields=id,name,email&access_token=XXXXX

默认情况下,Microsoft FB客户端实现将access_token作为“?access_token”附加到查询字符串,这会导致请求损坏(额外问号):

https://graph.facebook.com/v2.4/me?fields=id,email?access_token=XXXXX

因此,为了弥补我们需要使用自定义BackchannelHttpHandler.首先,我们创建端点类:

public class FacebookBackChannelHandler : HttpClientHandler
    {
        protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,CancellationToken cancellationToken)
        {
            if (!request.RequestUri.AbsolutePath.Contains("/oauth"))
            {
                request.RequestUri = new Uri(request.RequestUri.AbsoluteUri.Replace("?access_token","&access_token"));
            }

            return await base.SendAsync(request,cancellationToken);
        }
    }

然后我们在facebook auth选项中提供它,并显式指定UserInformationEndpoint:

var facebookAuthOptions = new FacebookAuthenticationOptions
        {
            AppId = ConfigurationManager.AppSettings["FacebookAppId"],AppSecret = ConfigurationManager.AppSettings["FacebookAppSecret"],BackchannelHttpHandler = new FacebookBackChannelHandler(),UserInformationEndpoint = "https://graph.facebook.com/v2.4/me?fields=id,email",Scope = { "email" }
            <.....>
        };

来自:https://stackoverflow.com/a/32636149/3130094

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读