asp.net-mvc-5 – 在默认的MVC5应用程序的帐户关联步骤中,从外部提供商Google和Facebook获取电子邮件

显然,您可以通过向Startup.Auth.cs中的FacebookAuthenticationoptions对象添加范围来通过Facebook提供者执行此操作:

http://blogs.msdn.com/b/webdev/archive/2013/10/16/get-more-information-from-social-providers-used-in-the-vs-2013-project-templates.aspx

List<string> scope = new List<string>() { "email" };
var x = new FacebookAuthenticationoptions();
x.Scope.Add("email");
...
app.UseFacebookAuthentication(x);

如何与Google提供商一样? GoogleAuthenticationoptions类/对象没有x.Scope属性

解决方法

请在本帖子的底部看到更新!

以下适用于我的Facebook:

StartupAuth.cs:

var facebookAuthenticationoptions = new FacebookAuthenticationoptions()
{
    AppId = "x",AppSecret = "y"
};
facebookAuthenticationoptions.Scope.Add("email");
app.UseFacebookAuthentication(facebookAuthenticationoptions);

ExternalLoginCallback方法

var externalIdentity = HttpContext.GetowinContext().Authentication.GetExternalIdentityAsync(DefaultAuthenticationTypes.ExternalCookie);
var emailClaim = externalIdentity.Result.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Email);
var email = emailClaim.Value;

而对于Google:

StartupAuth.cs

app.UseGoogleAuthentication();

ExternalLoginCallback方法(与Facebook相同):

var externalIdentity = HttpContext.GetowinContext().Authentication.GetExternalIdentityAsync(DefaultAuthenticationTypes.ExternalCookie);
var emailClaim = externalIdentity.Result.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Email);
var email = emailClaim.Value;

如果我在这里设置一个断点:

var email = emailClaim.Value;

我在调试器中看到Facebook和Google的电子邮件地址。

更新1:旧的答案让我困惑,所以我更新了我在我自己的项目中的代码,我刚刚调试,我知道工作。

更新2:使用新的ASP.NET Identity 2.0 RTM版本,您不再需要此帖中的任何代码获取电子邮件的正确方法是简单地执行以下操作:

> Startup.Auth.cs

app.UseFacebookAuthentication(
       appId: "x",appSecret: "y");

    app.UseGoogleAuthentication();

> AccountController.cs

//
// GET: /Account/ExternalLoginCallback
[AllowAnonymous]
public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
{
    var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
    if (loginInfo == null)
    {
        return RedirectToAction("Login");
    }

    // Sign in the user with this external login provider if the user already has a login
    var result = await SignInHelper.ExternalSignIn(loginInfo,isPersistent: false);
    switch (result)
    {
        case SignInStatus.Success:
            return RedirectToLocal(returnUrl);
        case SignInStatus.LockedOut:
            return View("Lockout");
        case SignInStatus.RequiresTwoFactorAuthentication:
            return RedirectToAction("SendCode",new { ReturnUrl = returnUrl });
        case SignInStatus.Failure:
        default:
            // If the user does not have an account,then prompt the user to create an account
            ViewBag.ReturnUrl = returnUrl;
            ViewBag.LoginProvider = loginInfo.Login.LoginProvider;
            return View("ExternalLoginConfirmation",new ExternalLoginConfirmationviewmodel { Email = loginInfo.Email });
    }
}

相关文章

这篇文章主要讲解了“WPF如何实现带筛选功能的DataGrid”,文...
本篇内容介绍了“基于WPF如何实现3D画廊动画效果”的有关知识...
Some samples are below for ASP.Net web form controls:(fr...
问题描述: 对于未定义为 System.String 的列,唯一有效的值...
最近用到了CalendarExtender,结果不知道为什么发生了错位,...
ASP.NET 2.0 page lifecyle ASP.NET 2.0 event sequence cha...