asp.Net   发布时间:2022-04-07  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了asp.net-mvc – 访问OAuth中的电子邮件地址ExternalLoginCallback从Facebook v2.4 ASP.NET MVC 5中的API [复制]大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
参见英文答案 > Why new fb api 2.4 returns null email on MVC 5 with Identity and oauth 2?5个答案使用Facebook API v2.3,如果设置了以下设置,用户的电子邮件地址将返回到ExternalLoginCallBACk;
app.UseFacebookAuthentication(new FacebookAuthenticationOptions
{
    AppId = "XXX",AppSecret = "XXX",Scope = { "email" }
});

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

我认为这可能与@L_772_1@所列的v2.4变更有关;

如何才能立即访问此电子邮件地址?

解决方法

要解决这个问题,我不得不从nuget安装 Facebook 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",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 – 访问OAuth中的电子邮件地址ExternalLoginCallback从Facebook v2.4 ASP.NET MVC 5中的API [复制]全部内容,希望文章能够帮你解决asp.net-mvc – 访问OAuth中的电子邮件地址ExternalLoginCallback从Facebook v2.4 ASP.NET MVC 5中的API [复制]所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。