public async Task <ActionResult> LinkLoginCallback() { var user = await GetCurrentUserAsync(); if (user == null) { return(View("Error")); } var info = await _signInManager.GetExternalLoginInfoAsync(await _userManager.GetUserIdAsync(user)); if (info == null) { return(RedirectToAction(nameof(ManageLogins), new { Message = ManageMessageId.Error })); } var result = await _userManager.AddLoginAsync(user, info); var message = result.Succeeded ? ManageMessageId.AddLoginSuccess : ManageMessageId.Error; //Update user with information from his Google Profile user.FullName = info.Principal.FindFirstValue(ClaimTypes.Name); user.PictureUrl = await _pictureLocator.GetProfilePictureAsync(info); await _userManager.UpdateAsync(user); return(RedirectToAction(nameof(ManageLogins), new { Message = message })); }
public async Task <IActionResult> LinkLoginCallback() { var user = await _userManager.GetUserAsync(User); if (user == null) { throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'."); } var info = await _signInManager.GetExternalLoginInfoAsync(user.Id); if (info == null) { throw new ApplicationException($"Unexpected error occurred loading external login info for user with ID '{user.Id}'."); } var result = await _userManager.AddLoginAsync(user, info); if (!result.Succeeded) { throw new ApplicationException($"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."; user.FullName = info.Principal.FindFirstValue(ClaimTypes.Name); user.PictureUrl = await _pictureLocator.GetProfilePictureAsync(info); await _userManager.UpdateAsync(user); return(RedirectToAction(nameof(ExternalLogins))); }
public async Task <IActionResult> ExternalLoginConfirmation(ExternalLoginViewModel model, string returnUrl = null) { if (ModelState.IsValid) { // Get the information about the user from the external login provider var info = await _signInManager.GetExternalLoginInfoAsync(); if (info == null) { throw new ApplicationException("Error loading external login information during confirmation."); } var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; user.FullName = info.Principal.FindFirstValue(ClaimTypes.Name); user.PictureUrl = await _pictureLocator.GetProfilePictureAsync(info); var result = await _userManager.CreateAsync(user); if (result.Succeeded) { result = await _userManager.AddLoginAsync(user, info); if (result.Succeeded) { await _signInManager.SignInAsync(user, isPersistent : false); _logger.LogInformation("User created an account using {Name} provider.", info.LoginProvider); return(RedirectToLocal(returnUrl)); } } AddErrors(result); } ViewData["ReturnUrl"] = returnUrl; return(View(nameof(ExternalLogin), model)); }