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

前端之家收集整理的这篇文章主要介绍了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

  1. List<string> scope = new List<string>() { "email" };
  2. var x = new FacebookAuthenticationOptions();
  3. x.Scope.Add("email");
  4. ...
  5. app.UseFacebookAuthentication(x);

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

解决方法

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

以下适用于我的Facebook:

StartupAuth.cs:

  1. var facebookAuthenticationOptions = new FacebookAuthenticationOptions()
  2. {
  3. AppId = "x",AppSecret = "y"
  4. };
  5. facebookAuthenticationOptions.Scope.Add("email");
  6. app.UseFacebookAuthentication(facebookAuthenticationOptions);

ExternalLoginCallback方法

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

而对于Google:

StartupAuth.cs

  1. app.UseGoogleAuthentication();

ExternalLoginCallback方法(与Facebook相同):

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

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

  1. var email = emailClaim.Value;

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

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

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

> Startup.Auth.cs

  1. app.UseFacebookAuthentication(
  2. appId: "x",appSecret: "y");
  3.  
  4. app.UseGoogleAuthentication();

> AccountController.cs

  1. //
  2. // GET: /Account/ExternalLoginCallback
  3. [AllowAnonymous]
  4. public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
  5. {
  6. var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
  7. if (loginInfo == null)
  8. {
  9. return RedirectToAction("Login");
  10. }
  11.  
  12. // Sign in the user with this external login provider if the user already has a login
  13. var result = await SignInHelper.ExternalSignIn(loginInfo,isPersistent: false);
  14. switch (result)
  15. {
  16. case SignInStatus.Success:
  17. return RedirectToLocal(returnUrl);
  18. case SignInStatus.LockedOut:
  19. return View("Lockout");
  20. case SignInStatus.RequiresTwoFactorAuthentication:
  21. return RedirectToAction("SendCode",new { ReturnUrl = returnUrl });
  22. case SignInStatus.Failure:
  23. default:
  24. // If the user does not have an account,then prompt the user to create an account
  25. ViewBag.ReturnUrl = returnUrl;
  26. ViewBag.LoginProvider = loginInfo.Login.LoginProvider;
  27. return View("ExternalLoginConfirmation",new ExternalLoginConfirmation@R_403_93@l { Email = loginInfo.Email });
  28. }
  29. }

猜你在找的asp.Net相关文章