public ActionResult ChangeMyPassword(ChangeMyPasswordViewModel model, string returnUrl) { ViewBag.ReturnUrl = returnUrl; ViewBag.PageTitle = "USER MANAGEMENT"; Dictionary <string, string> PanelTitles = new Dictionary <string, string>(); PanelTitles["PanelTitle1"] = "CHANGE MY PASSWORD"; ViewBag.PanelTitles = PanelTitles; if (!ModelState.IsValid) { ViewBag.ErrorMessage = "Please check the inputs"; return(View(model)); } IdentityManager im = new IdentityManager(); string userId = User.Identity.GetUserId(); //string userId = "398574f6-c390-4f76-b6a8-84109e330f46"; if (im.ChangePassword(userId, model.OldPassword, model.NewPassword)) { im.DisablePasswordChangeFlag(userId); ViewBag.SuccessMessage = "Password Successfully Changed"; } else { ViewBag.ErrorMessage = "Can't Change the Password"; } return(View()); }
public async Task <ActionResult> MyChangePasswordSave(ChangeMyPasswordViewModel modelo, FormCollection formCollection) { try { if (ModelState.IsValid) { var usuarioService = Service as IUsuarioService; var loginResult = await usuarioService.ChangePassword(modelo.PasswordCurrent, modelo.Password); if (loginResult.Succeeded) { return(new JsonResult { Data = new { success = true } }); } ModelState.AddModelError("", string.Join(", ", loginResult.Errors)); } } catch (Exception ex) { var result = ManejadorExcepciones.HandleException(ex); ModelState.AddModelError("", result.Message); } return(new JsonResult { Data = new { success = false, errors = ModelState.ToSerializedDictionary() } }); }
public ActionResult Authorize(ChangeMyPasswordViewModel changeMyPasswordViewModel) { ChangeMyPasswordViewModel cmp = changeMyPasswordViewModel; if (cmp.NewPassword1 != cmp.NewPassword2) { changeMyPasswordViewModel.ErrorMessage = "Passwords do not match!"; return(View("Edit", changeMyPasswordViewModel)); } using (DataAccessLayer.SenecaContext db = new DataAccessLayer.SenecaContext()) { if (PageExpired) { changeMyPasswordViewModel.ErrorMessage = "Page expired."; return(View("Edit", changeMyPasswordViewModel)); } if (Session["userId"] == null) { // ReSharper disable once Mvc.ActionNotResolved // ReSharper disable once Mvc.ControllerNotResolved return(RedirectToAction("Index", "Login")); } db.SaveChanges(); changeMyPasswordViewModel.ErrorMessage = "PASSWORD SUCCESFULLY CHANGED."; PageExpired = true; return(View("Edit", changeMyPasswordViewModel)); } }
[CustomeAuthorizeForAjaxAndNonAjax] //The below method is called using ajax request. To authorize it, use this custome attribute. Authorize it for logged in users without any role. public async Task <IActionResult> ChangeMyPassword(ChangeMyPasswordViewModel model) { if (ModelState.IsValid) { //bring the current logged in user's info using the GetUserAsync(User) whereas "User" is a predefined parameter in microsoft identity to represent the logged in user. var user = await _userManager.GetUserAsync(User); if (user == null) { //return RedirectToAction("Login", "Account"); //we are calling this method using ajax request, so it is hard to redirect without specific modifications, so: return(NotFound()); } // ChangePasswordAsync changes the user password var result = await _userManager.ChangePasswordAsync(user, model.CurrentPassword, model.NewPassword); // The new password did not meet the complexity rules or // the current password is incorrect. Add these errors to // the ModelState and rerender ChangePassword view if (!result.Succeeded) { foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } return(Json(new { isValid = false, html = SerializeHtmlElemtnsToString.RenderRazorViewToString(this, "ChangeMyPassword", model) })); } // Upon successfully changing the password refresh sign-in cookie await _signInManager.RefreshSignInAsync(user); var AccountProfileViewModel = new AccountProfileViewModel { Id = user.Id, DisplayName = user.DisplayName, UserName = user.UserName, Email = user.Email }; return(Json(new { isValid = true, html = SerializeHtmlElemtnsToString.RenderRazorViewToString(this, "_ViewAll", AccountProfileViewModel) })); } //if the model submitted is not valid according to the Attriburtes in [] in the model file in Models folder: return(Json(new { isValid = false, html = SerializeHtmlElemtnsToString.RenderRazorViewToString(this, "ChangeMyPassword", model) })); }
[CustomeAuthorizeForAjaxAndNonAjax] //The below method is called using ajax request. To authorize it, use this custome attribute. Authorize it for logged in users without any role. public async Task <IActionResult> ChangeMyPassword() { //bring the current logged in user's info using the GetUserAsync(User) whereas "User" is a predefined parameter in microsoft identity to represent the logged in user. var user = await _userManager.GetUserAsync(User); if (user == null) { //return RedirectToAction("Login", "Account"); //we are calling this method using ajax request, so it is hard to redirect without specific modifications, so: return(NotFound()); } var ChangeMyPasswordViewModel = new ChangeMyPasswordViewModel { CurrentPassword = "", NewPassword = "", ConfirmPassword = "" }; return(View(ChangeMyPasswordViewModel)); }