public async Task <IActionResult> Index(ManageVM model) { if (!ModelState.IsValid) { return(View(model)); } var user = await GetLoggedInUserAsync(); // Update the user with the values from the model user.Email = model.Email; user.PhoneNumber = model.PhoneNumber; user.FirstName = model.FirstName; user.LastName = model.LastName; var updateResult = await _accountService.UpdateUserAsync(user); if (!updateResult.Succeeded) { throw new ApplicationException(@"Unexpected error occurred updating user"); } StatusMessage = new StatusMessageVM(true, "Your profile has been updated"); return(RedirectToAction(nameof(Index))); }
public async Task <IActionResult> SendVerificationEmail(ManageVM model) { if (!ModelState.IsValid) { return(View(model)); } var user = await GetLoggedInUserAsync(); var code = await _accountService.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = Url.EmailConfirmationLink(user.Id, code, Request.Scheme); var email = user.Email; var response = await _notificationService.SendConfirmationNotificationAsync(user, callbackUrl); if (response.Success) { StatusMessage = new StatusMessageVM(true, "Verification email sent. Please check your email."); } else { StatusMessage = new StatusMessageVM(false, "An error occurred"); } return(RedirectToAction(nameof(Index))); }
public async Task <IActionResult> ChangePassword(ChangePasswordVM model) { if (!ModelState.IsValid) { return(View(model)); } var user = await GetLoggedInUserAsync(); if (ViewHelper.GetIdentityProvider(User.Claims) != "local") { throw new ApplicationException($"Cannot set a password for a non-local user '{User.Identity.Name}'."); } var changePasswordResult = await _accountService.ChangePasswordAsync(user, model.OldPassword, model.NewPassword); if (!changePasswordResult.Succeeded) { AddErrors(changePasswordResult.Errors); return(View(model)); } await _accountService.SignInUserAsync(user, isPersistent : false); _logger.LogInformation("User changed their password successfully."); StatusMessage = new StatusMessageVM(true, "Your password has been changed."); return(RedirectToAction(nameof(ChangePassword))); }
public async Task <IActionResult> UpdateRole(UserRolesVM model) { if (ModelState.IsValid) { if (User.Claims.FirstOrDefault(x => x.Type == "sub")?.Value == model.Id) { StatusMessage = new StatusMessageVM(false, "Cannot update the logged-in user's role"); return(RedirectToAction("Index")); } var addToRoles = new List <string>(); var removeFromRoles = new List <string>(); foreach (var r in model.UserRoles) { if (r.IsChecked) { addToRoles.Add(r.Id); } else { removeFromRoles.Add(r.Id); } } await _accountService.UpdateUserRolesAsync(model.Id, addToRoles, removeFromRoles); StatusMessage = new StatusMessageVM(true, "User Roles Updated for " + model.UserName); return(RedirectToAction("Index")); } var user = await _accountService.FindUserByIdAsync(model.Id); var rolesForUser = await _accountService.GetRolesForUserAsync(user); var m = PopulateUserRolesViewModel(user, rolesForUser); return(View("UpdateRole", m)); }