在默认MVC5应用程序中的帐户关联步骤中从外部提供商Google和Facebook获取电子邮件

Pus*_*ots 25 asp.net-mvc-5 asp.net-identity asp.net-identity-2

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

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);
Run Code Online (Sandbox Code Playgroud)

如何与Google提供商进行相同的操作?类/对象没有x.Scope属性GoogleAuthenticationOptions!

Pus*_*ots 47

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

以下适合我的Facebook:

StartupAuth.cs:

var facebookAuthenticationOptions = new FacebookAuthenticationOptions()
{
    AppId = "x",
    AppSecret = "y"
};
facebookAuthenticationOptions.Scope.Add("email");
app.UseFacebookAuthentication(facebookAuthenticationOptions);
Run Code Online (Sandbox Code Playgroud)

ExternalLoginCallback方法:

var externalIdentity = HttpContext.GetOwinContext().Authentication.GetExternalIdentityAsync(DefaultAuthenticationTypes.ExternalCookie);
var emailClaim = externalIdentity.Result.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Email);
var email = emailClaim.Value;
Run Code Online (Sandbox Code Playgroud)

而对于谷歌:

StartupAuth.cs

app.UseGoogleAuthentication();
Run Code Online (Sandbox Code Playgroud)

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;
Run Code Online (Sandbox Code Playgroud)

如果我在这里设置断点:

var email = emailClaim.Value;
Run Code Online (Sandbox Code Playgroud)

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

更新1:旧答案让我感到困惑所以我用自己项目中的代码更新了它,我刚刚调试过,我知道有效.

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

  1. Startup.Auth.cs

        app.UseFacebookAuthentication(
           appId: "x",
           appSecret: "y");
    
        app.UseGoogleAuthentication();
    
    Run Code Online (Sandbox Code Playgroud)
  2. 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 });
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)

  • 我已经尝试过使用Microsoft.AspNet.Identity.Core.2.2.1的此解决方案,FB auth和email始终为null ..它仍然是正确的方法吗? (5认同)
  • 关于更新2 - 我仍然必须使用Scope.Add("email")代码来获取用户的电子邮件地址.默认情况下,我认为您只能获得*public*个人资料信息. (4认同)
  • 我有一个Email属性,但它仍然是null. (4认同)
  • 那么我们可以使用该电子邮件地址注册用户,而不是要求他们提供用户名吗? (2认同)
  • 是的,你可以这样做.我已经在我自己的webapp中完成了这个.工作良好. (2认同)

ped*_*sky 6

您需要显式配置FacebookAuthenticationOptions以从经过身份验证的用户获取电子邮件地址.

在MVC5项目中,在Startup.Auth.cs中添加这些行

        var options = new FacebookAuthenticationOptions() {
            AppId = "xxxxxxxx",
            AppSecret = "xxxxxxxxx"
        };
        options.Scope.Add("email");
        app.UseFacebookAuthentication(options);
Run Code Online (Sandbox Code Playgroud)

更新 将我的示例代码减少到绝对最小值.你的更新代码顺便说一下,我也试过Facebook和谷歌.