public ActionResult ChangeAccountData(ChangeAccountDataViewModel adminAccount) { if (ModelState.IsValid) { if (repository.SaveAdminAccount(adminAccount.Login, adminAccount.Password, System.Web.HttpContext.Current.User.Identity.Name, adminAccount.OldPassword)) { TempData["message"] = string.Format("Изменения логина/пароля успешно сохранены"); return(RedirectToAction("Index", "Admin")); } TempData["errorMessage"] = string.Format("Ошибка! Введённый логин или старый пароль неверны!"); return(View()); } return(View()); }
public async Task <ActionResult> ChangeAccountData(ChangeAccountDataViewModel model) { if (!ModelState.IsValid) { return(View("Index", model)); } var id = ((ClaimsIdentity)User.Identity).Claims.ToList().First(x => x.Type == ClaimTypes.NameIdentifier).Value; var updateViewModel = new UpdateViewModel { Id = id, }; if (!string.IsNullOrWhiteSpace(model.EmailAddress)) { updateViewModel.Email = model.EmailAddress; } // TODO: validate pw // TODO: old pw? what if non exists? if (!string.IsNullOrEmpty(model.NewPassword)) { updateViewModel.Password = model.NewPassword; updateViewModel.ConfirmPassword = model.ConfirmPassword; } try { await Client.Update(updateViewModel); // update claims to provide the user up-to-date data var identity = (ClaimsIdentity)User.Identity; var context = Request.GetOwinContext(); var claim = identity.FindFirst(ClaimTypes.Email); if (!string.IsNullOrWhiteSpace(model.EmailAddress) && claim.Value != model.EmailAddress) { identity.RemoveClaim(claim); identity.AddClaim(new Claim(ClaimTypes.Email, updateViewModel.Email)); } context.Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie); context.Authentication.SignIn(identity); } catch (ApiException e) { ModelState.AddModelError("", e.Message); } return(RedirectToAction("Index", model)); }
public async Task <IActionResult> ChangeAccountData(ChangeAccountDataViewModel viewModel) { var user = await _userManager.FindByIdAsync(viewModel.Id); if (user != null) { try { user.UserName = viewModel.Username; user.PhoneNumber = viewModel.PhoneNumber; await _userManager.UpdateAsync(user); return(RedirectToAction("AccountOverview")); } catch (Exception ex) { return(View("Error")); } } else { return(View("Error")); } }