public async Task <IActionResult> OnGetCallback() { var result = await HttpContext.AuthenticateAsync(IdentityConstants.ExternalScheme); if (result?.Succeeded != true) { throw new Exception("External authentication error"); } var(user, provider, providerUserId, claims) = await FindUserFromExternalProviderAsync(result); var returnUrl = result.Properties.Items["returnUrl"]; if (user == null) { var email = result.Principal.Claims.FindEmail(); if (email != null && (user = await _userManager.FindByEmailAsync(email)) == null) { // no user found, just add the login to the current user var currentUser = await _userManager.GetUserAsync(User); await TryConnectExternalLoginToUser(currentUser, providerUserId, result); return(ReturnToUrl(returnUrl)); } } // either we have: // * identified a user with the remote login or // * the remote login has an email that matches a local user record or // * we have not found any matching user because missing email claim return(await HandleMerge(user, returnUrl)); }
private async Task <IdentityUser> FindUser(AuthenticateResult result) { // For create flow when we have user-id present if (result.Properties.Items.TryGetValue("userId", out var userId) && userId != null) { return(await _userManager.FindByIdAsync(userId)); } // For login flow when we do not have a user-id var email = result.Principal.Claims.FindEmail(); if (email != null) { return(await _userManager.FindByEmailAsync(email)); } return(null); }