public ActionResult Manage(ChangePropertyModel model) { bool hasLocalAccount = OAuthWebSecurity.HasLocalAccount(MySqlWebSecurity.GetUserId(User.Identity.Name)); ViewBag.HasLocalPassword = hasLocalAccount; ViewBag.ReturnUrl = Url.Action("Manage"); if (hasLocalAccount) { if (ModelState.IsValid) { // ChangePassword will throw an exception rather than return false in certain failure scenarios. bool changePasswordSucceeded = false; if (string.IsNullOrEmpty(model.LocalPasswordModel.ConfirmPassword) == true) { using (var db = MySqlSimpleMembershipDbContext.CreateContext()) { var userProperty = db.UserProperties.SingleOrDefault(x => x.UserName == User.Identity.Name); if (userProperty == null) { var userId = MySqlWebSecurity.GetUserId(User.Identity.Name); userProperty = new UserProperty { UserId = userId, UserName = User.Identity.Name, }; db.UserProperties.Add(userProperty); } TicketSQLRepository.UpdateEmail(userProperty.Email, model.PropertyModel.Email); userProperty.Email = model.PropertyModel.Email; userProperty.FirstName = model.PropertyModel.FirstName; userProperty.LastName = model.PropertyModel.LastName; changePasswordSucceeded = db.SaveChanges() > 0; } } else { try { changePasswordSucceeded = MySqlWebSecurity.ChangePassword(User.Identity.Name, model.LocalPasswordModel.OldPassword, model.LocalPasswordModel.NewPassword); } catch (Exception) { changePasswordSucceeded = false; } } if (changePasswordSucceeded == true) { return RedirectToAction("Manage", new { Message = /*ManageMessageId.ChangePasswordSuccess*/"Settings are saved" }); } else { /*ModelState.AddModelError("", "The current password is incorrect or the new password is invalid.");*/ ModelState.AddModelError("", "The settings could not be saved."); } } } else { // User does not have a local password so remove any validation errors caused by a missing // OldPassword field ModelState state = ModelState["OldPassword"]; if (state != null) { state.Errors.Clear(); } if (ModelState.IsValid) { try { MySqlWebSecurity.CreateAccount(User.Identity.Name, model.LocalPasswordModel.NewPassword); return RedirectToAction("Manage", new { Message = ManageMessageId.SetPasswordSuccess }); } catch (Exception e) { ModelState.AddModelError("", e); } } } // If we got this far, something failed, redisplay form return View(model); }
// // GET: /Account/Manage public ActionResult Manage(ManageMessageId? message) { ViewBag.StatusMessage = message == ManageMessageId.ChangePasswordSuccess ? "Your password has been changed." : message == ManageMessageId.SetPasswordSuccess ? "Your password has been set." : message == ManageMessageId.RemoveLoginSuccess ? "The external login was removed." : message == ManageMessageId.RequestOneExternalLogin ? "You must one external login or local account." : ""; ViewBag.HasLocalPassword = OAuthWebSecurity.HasLocalAccount(MySqlWebSecurity.GetUserId(User.Identity.Name)); ViewBag.ReturnUrl = Url.Action("Manage"); var model = new ChangePropertyModel { LocalPasswordModel = new LocalPasswordModel(), PropertyModel = new PropertyModel(), }; using (var db = MySqlSimpleMembershipDbContext.CreateContext()) { var userProperties = db.UserProperties.SingleOrDefault(x => x.UserName == User.Identity.Name); if (userProperties != null) { model.PropertyModel = new PropertyModel { Email = userProperties.Email, FirstName = userProperties.FirstName, LastName = userProperties.LastName, }; } } return View(model); }