6
votes

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?

1

1 Answers

3
votes

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.