public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } var user = await _userManager.GetUserAsync(User); if (user == null) { return(NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.")); } var changePasswordResult = await _userManager.ChangePasswordAsync(user, Input.OldPassword, Input.NewPassword); if (!changePasswordResult.Succeeded) { foreach (var error in changePasswordResult.Errors) { ModelState.AddModelError(string.Empty, error.Description); } return(Page()); } await _signInManager.RefreshSignInAsync(user); _logger.LogInformation("User changed their password successfully."); StatusMessage = "Your password has been changed."; return(RedirectToPage()); }
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) { StatusMessage = "The external login was not removed."; return(RedirectToPage()); } await _signInManager.RefreshSignInAsync(user); StatusMessage = "The external login was removed."; return(RedirectToPage()); }
public async Task <IActionResult> OnPostAsync() { var user = await _userManager.GetUserAsync(User); if (user == null) { return(NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.")); } if (!ModelState.IsValid) { await LoadAsync(user); return(Page()); } var phoneNumber = await _userManager.GetPhoneNumberAsync(user); if (Input.PhoneNumber != phoneNumber) { var setPhoneResult = await _userManager.SetPhoneNumberAsync(user, Input.PhoneNumber); if (!setPhoneResult.Succeeded) { var userId = await _userManager.GetUserIdAsync(user); throw new InvalidOperationException($"Unexpected error occurred setting phone number for user with ID '{userId}'."); } } if (user.GivenName != Input.GivenName || user.FamilyName != Input.FamilyName) { user.GivenName = Input.GivenName; user.FamilyName = Input.FamilyName; await _userManager.UpdateAsync(user); } await _signInManager.RefreshSignInAsync(user); StatusMessage = "Your profile has been updated"; return(RedirectToPage()); }
public async Task <IActionResult> OnPostAsync() { var user = await _userManager.GetUserAsync(User); if (user == null) { return(NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.")); } await _userManager.SetTwoFactorEnabledAsync(user, false); await _userManager.ResetAuthenticatorKeyAsync(user); _logger.LogInformation("User with ID '{UserId}' has reset their authentication app key.", user.Id); await _signInManager.RefreshSignInAsync(user); StatusMessage = "Your authenticator app key has been reset, you will need to configure your authenticator app using the new key."; return(RedirectToPage("./EnableAuthenticator")); }
public async Task <IActionResult> OnGetAsync(string userId, string email, string code) { if (userId == null || email == null || code == null) { return(RedirectToPage("/Index")); } var user = await _userManager.FindByIdAsync(userId); if (user == null) { return(NotFound($"Unable to load user with ID '{userId}'.")); } code = Encoding.UTF8.GetString(WebEncoders.Base64UrlDecode(code)); var result = await _userManager.ChangeEmailAsync(user, email, code); if (!result.Succeeded) { StatusMessage = "Error changing email."; return(Page()); } // In our UI email and user name are one and the same, so when we update the email // we need to update the user name. var setUserNameResult = await _userManager.SetUserNameAsync(user, email); if (!setUserNameResult.Succeeded) { StatusMessage = "Error changing user name."; return(Page()); } await _signInManager.RefreshSignInAsync(user); StatusMessage = "Thank you for confirming your email change."; return(Page()); }
public async Task <IActionResult> OnPostAsync() { var user = await _userManager.GetUserAsync(User); if (user == null) { return(NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.")); } if (!ModelState.IsValid) { await LoadAsync(user); return(Page()); } var identity = await _userClaimsPrincipalFactory.CreateAsync(user); var currentClaim = identity.Claims.FirstOrDefault(c => c.Type == "permission" && c.Value == "can_read"); if (Input.CanRead != (currentClaim?.Value == "can_read")) { if (Input.CanRead) { await _userManager.AddClaimAsync(user, new Claim("permission", "can_read")); } else { await _userManager.RemoveClaimAsync(user, currentClaim); } } await _signInManager.RefreshSignInAsync(user); StatusMessage = "Your permissions have been updated"; return(RedirectToPage()); }