public async Task <IActionResult> OnPostRemoveLoginAsync(string loginProvider, string providerKey) { var user = await _userManager.GetUserAsync(User); if (user == null) { return(NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.")); } var result = await _userManager.RemoveLoginAsync(user, loginProvider, providerKey); if (!result.Succeeded) { var userId = await _userManager.GetUserIdAsync(user); throw new InvalidOperationException($"Unexpected error occurred removing external login for user with ID '{userId}'."); } await _signInManager.RefreshSignInAsync(user); StatusMessage = "The external login was removed."; return(RedirectToPage()); }
public async Task MergeUsers(TUser currentUser, TUser user) { var observer = new IdentityResultObserver(); using (var transaction = new TransactionScope( TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted, Timeout = TransactionManager.DefaultTimeout }, TransactionScopeAsyncFlowOption.Enabled) ) { var roles = await _userManager.GetRolesAsync(user); var userLogins = await _userManager.GetLoginsEmailInfoAsync(user); var claims = await _userManager.GetClaimsAsync(user); var emails = _userManager.GetEmails(user); var existingRoles = await _userManager.GetRolesAsync(currentUser); var missingRoles = roles.Where(role => !existingRoles.Contains(role)); await observer.Observe(async() => await _userManager.AddToRolesAsync(currentUser, missingRoles)); foreach (var login in userLogins) { await observer.Observe(async() => { await _userManager.RemoveLoginAsync(user, login.UserLoginInfo.LoginProvider, login.UserLoginInfo.ProviderKey); return(await _userManager.AddLoginAsync(currentUser, login.UserLoginInfo, login.Email)); }); } foreach (var email in emails.Where(e => !userLogins.Any(l => l.Email.Equals(e.Email, StringComparison.InvariantCultureIgnoreCase)))) { await observer.Observe(async() => await _userManager.AddEmailAsync(currentUser, email.Email, email.UserLoginInfo)); } if (_options.Value.MergeUnconfirmedEmails || await _userManager.IsEmailConfirmedAsync(user)) { var email = await _userManager.GetEmailAsync(user); await observer.Observe(async() => await _userManager.AddEmailAsync(currentUser, email)); } var existingClaims = await _userManager.GetClaimsAsync(currentUser); var missingClaims = claims.Where(c => !existingClaims.Any(ec => c.Type.Equals(ec.Type, StringComparison.InvariantCultureIgnoreCase) && c.Value.Equals(ec.Value, StringComparison.InvariantCultureIgnoreCase) )); await observer.Observe(async() => await _userManager.AddClaimsAsync(currentUser, missingClaims)); await observer.Observe(async() => await _userManager.DeleteAsync(user)); await _mergeUserEvents.MergeUser(currentUser, user); transaction.Complete(); } }