Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AspNet Identity multiple external login with same email

Visual Studio-generates a bunch of code for AspNet Identity, namely the LoginController and ManageController. In the ManageController there is the following code:

    [HttpGet, Route("Manage/LinkLoginCallback")]
    public async Task<ActionResult> LinkLoginCallback(string returnUrl)
    {
        ExternalLoginInfo loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync(XsrfKey, User.Identity.GetUserId());

        ...
        var result = await UserManager.AddLoginAsync(User.Identity.GetUserId(), loginInfo.Login);

        if (result.Succeeded) 
        {
            ...
        }
    }

The problem I'm having with this code is that UserManager.AddLoginAsync() throws an exception when an external login with an existing email address is added. However, this seems to be a valid scenario:

  1. User signed up to Google Plus with email: [email protected]
  2. User used same email to sign up for Facebook.
  3. Now they cannot "Link" both accounts under a single local account in my application because of this exception.

How do I handle this situation and add both accounts? Are there any problems with doing this?

like image 662
parliament Avatar asked Nov 25 '14 04:11

parliament


1 Answers

You can disable the requirement for unique email like this:

    manager.UserValidator = new UserValidator<ApplicationUser>(manager)
        {
            RequireUniqueEmail = false
        }; 

Where manager is an instance of UserManager class.

like image 115
Riwen Avatar answered Oct 15 '22 11:10

Riwen