AspNet Identity multiple external logins with the same email address

Visual Studio - Generates a bunch of code for AspNet Identity, namely LoginController and ManageController. The ManageController has 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 with this code is that UserManager.AddLoginAsync() throws an exception when an external login with an existing email address is added. However, this is apparently the correct scenario:

  • A user subscribed to Google Plus via email: user-email@gmail.com
  • The user used the same email address to subscribe to Facebook.
  • Now they cannot β€œlink” both accounts under the same local account in my application due to this exception.

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

+5
source share
1 answer

You can disable the unique email requirement as follows:

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

Where manager is an instance of the UserManager class.

+2
source

Source: https://habr.com/ru/post/1207634/


All Articles