public async Task <IActionResult> OnGetLinkLoginCallbackAsync() { var user = await _userManager.GetUserAsync(User); if (user == null) { return(NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.")); } var info = await _signInManager.GetExternalLoginInfoAsync(await _userManager.GetUserIdAsync(user)); if (info == null) { throw new InvalidOperationException($"Unexpected error occurred loading external login info for user with ID '{user.Id}'."); } var logins = await _userManager.GetLoginsAsync(user); if (logins.Any(l => l.LoginProvider == info.LoginProvider && l.ProviderKey == info.ProviderKey)) { StatusMessage = "The external login is already linked to your account."; return(RedirectToPage()); } var email = info.Principal.Claims.FindEmail(); var result = await _userManager.AddLoginAsync(user, info, email); if (!result.Succeeded) { throw new InvalidOperationException($"Unexpected error occurred adding external login for user with ID '{user.Id}'."); } // Clear the existing external cookie to ensure a clean login process await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme); StatusMessage = "The external login was added."; return(RedirectToPage()); }