Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Associate a new Google or Facebook login with an existing account

I have just followed instructions in this article to add Google as a login provider to my MVC 5 app. All seems to work OK, but when I log in via Google, it wants me to register the email/username provided by Google as a new account in my app. If I leave the email as is and click the 'Register' button, it tells me that address is already taken, as I have earlier registered on my app's own login provider.

How can I tweak the default code generated by the MVC project template to allow me to associate the Google login with an existing local account?

P.S. I have exactly the same problem with Facebook.

like image 618
ProfK Avatar asked Sep 20 '15 14:09

ProfK


1 Answers

I totally second the points raised by @Chris Pratt However i'm not sure the code used is enough to do what the OP asked.

adding this code in the default block inside ExternalLoginCallback should do the job

ApplicationUser user = await UserManager.FindByNameAsync(loginInfo.Email);
if (user != null)
{
    var addLoginResult = await UserManager.AddLoginAsync(user.Id, loginInfo.Login);
    if (addLoginResult.Succeeded)
    {
        await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
        return RedirectToLocal(returnUrl);
    }
}
like image 157
Souhaieb Besbes Avatar answered Sep 28 '22 04:09

Souhaieb Besbes