Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the email from external providers Google and Facebook during account association step in a default MVC5 app

Tags:

Apparently you can do this with the Facebook provider by adding scopes to the FacebookAuthenticationOptions object in 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); 

How to do the same with Google provider? There isn't a x.Scope property for the GoogleAuthenticationOptions class/object!

like image 829
PussInBoots Avatar asked Dec 04 '13 14:12

PussInBoots


People also ask

How do I add Google authentication to ASP core application?

Sign in with GoogleRun the app and select Log in. An option to sign in with Google appears. Select the Google button, which redirects to Google for authentication. After entering your Google credentials, you are redirected back to the web site.

What is OAuth authentication in ASP NET MVC?

OAuth provides client applications a "secure delegated access" to server resources on behalf of a resource owner. It specifies a process for resource owners to authorize third-party access to their server resources without sharing their credentials (from the Wikipedia). OAuth stands for Open Authorization.


2 Answers

PLEASE SEE UPDATES AT THE BOTTOM OF THIS POST!

The following works for me for Facebook:

StartupAuth.cs:

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

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; 

And for Google:

StartupAuth.cs

app.UseGoogleAuthentication(); 

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; 

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

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.

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

        app.UseFacebookAuthentication(        appId: "x",        appSecret: "y");      app.UseGoogleAuthentication(); 
  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 });     } } 
like image 179
PussInBoots Avatar answered Sep 20 '22 14:09

PussInBoots


You need to explicitly configure the FacebookAuthenticationOptions to get the email address from the authenticated user.

In your MVC5 project, add these lines in the Startup.Auth.cs

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

Update Reduced my sample code to the absolute minimum. Your updated code works fine by the way, I have also tried it with both Facebook and Google.

like image 33
pedrusky Avatar answered Sep 20 '22 14:09

pedrusky