获取过程中默认MVC5应用帐户关联的步骤从外部供应商谷歌和Facebook的电子邮件 [英] Getting the email from external providers Google and Facebook during account association step in a default MVC5 app

查看:241
本文介绍了获取过程中默认MVC5应用帐户关联的步骤从外部供应商谷歌和Facebook的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

显然,你可以通过在 Startup.Auth.cs FacebookAuthenticationOptions 对象与Facebook的供应商做到这一点>:

Apparently you can do this with the Facebook provider by adding scopes to the FacebookAuthenticationOptions object in Startup.Auth.cs:

<一个href=\"http://blogs.msdn.com/b/webdev/archive/2013/10/16/get-more-information-from-social-providers-used-in-the-vs-2013-project-templates.aspx\" rel=\"nofollow\">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);

如何做到与谷歌提供商一样吗?没有一个 x.Scope 属性的 GoogleAuthenticationOptions 类/对象!

推荐答案

请查看更新这个帖子的底部!

下面的作品我的的Facebook

StartupAuth.cs:

StartupAuth.cs:

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

ExternalLoginCallback方式:

ExternalLoginCallback method:

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

和对于谷歌

StartupAuth.cs

StartupAuth.cs

app.UseGoogleAuthentication();

ExternalLoginCallback方法(同为Facebook):

ExternalLoginCallback method (same as for facebook):

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

如果我设置断点的位置:

If I set a breakpoint here:

var email = emailClaim.Value;

我看到的电子邮件地址,Facebook和谷歌都在调试器。

I see the email address for both Facebook and Google in the debugger.

更新1 :旧的答案让我困惑,所以我用code我有我自己的项目,我只是调试,我知道作品更新它。

Update 1: The old answer had me confused so I updated it with the code I have in my own project that I just debugged and I know works.

更新2 :随着新的<一个href=\"http://blogs.msdn.com/b/webdev/archive/2014/03/20/test-announcing-rtm-of-asp-net-identity-2-0-0.aspx\">ASP.NET身份2.0 RTM版本不再需要任何code在这个职位。正确的方式获得的电子邮件是简单地做以下内容:

Update 2: With the new ASP.NET Identity 2.0 RTM version you no longer need any of the code in this post. The proper way to get the email is by simply doing the following:


  1. Startup.Auth.cs

  1. Startup.Auth.cs

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

    app.UseGoogleAuthentication();


  • AccountController.cs

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


  • 这篇关于获取过程中默认MVC5应用帐户关联的步骤从外部供应商谷歌和Facebook的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆