private void btnChangeUserPassword_Click(object sender, EventArgs e) { try { bool status = userProvider.ChangePassword(txtUsername.Text, txtOldPassword.Text, txtNewPassword.Text); if (status) { MessageBox.Show("User password changed successfully", "Change user password", MessageBoxButtons.OK, MessageBoxIcon.Information); //loadUsers(); } else { MessageBox.Show("Failed to change user password", "Change user password", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } catch (ArgumentException ex) { MessageBox.Show("Failed to change user password, " + ex.Message, "Change user password", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } catch (SqlException ex) { MessageBox.Show("Failed to change user password, " + ex.Message, "Change user password", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } catch (Exception ex) { MessageBox.Show("Failed to change user password, " + ex.Message, "Change user password", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } }
public bool ChangePassword(string userName, string oldPassword, string newPassword) { if (String.IsNullOrEmpty(userName)) { throw new ArgumentException("值不能为 null 或为空。", "userName"); } if (String.IsNullOrEmpty(oldPassword)) { throw new ArgumentException("值不能为 null 或为空。", "oldPassword"); } if (String.IsNullOrEmpty(newPassword)) { throw new ArgumentException("值不能为 null 或为空。", "newPassword"); } // 在某些出错情况下,基础 ChangePassword() 将引发异常, // 而不是返回 false。 try { return(_provider.ChangePassword(userName, oldPassword, newPassword)); } catch (ArgumentException) { return(false); } catch (MembershipPasswordException) { return(false); } }
public static bool Save(string oldPass, string newPass) { string userName = System.Web.HttpContext.Current.User.Identity.Name; CustomMembershipProvider customMembership = new CustomMembershipProvider(); if (customMembership.ChangePassword(userName, oldPass, newPass)) { System.Web.Security.FormsAuthentication.SignOut(); return(true); } return(false); }
public ActionResult Manage(LocalPasswordModel model) { bool hasLocalAccount = false; var user = _unitOfWork.UserRepository.GetSingle(x => x.Id == MembershipContext.Current.User.Id && !x.Deleted && x.Active); if (user != null) { hasLocalAccount = true; } 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; try { var memberShip = new CustomMembershipProvider(); changePasswordSucceeded = memberShip.ChangePassword(User.Identity.Name, model.OldPassword, model.NewPassword); } catch (Exception) { changePasswordSucceeded = false; } if (changePasswordSucceeded) { return(RedirectToAction("Manage", new { Message = ManageMessageId.ChangePasswordSuccess })); } else { ModelState.AddModelError("", "The current password is incorrect or the new password is invalid."); } } } 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 { WebSecurity.CreateAccount(User.Identity.Name, model.NewPassword); return(RedirectToAction("Manage", new { Message = ManageMessageId.SetPasswordSuccess })); } catch (Exception) { ModelState.AddModelError("", String.Format("Unable to create local account. An account with the name \"{0}\" may already exist.", User.Identity.Name)); } } } // If we got this far, something failed, redisplay form return(View(model)); }
/// <summary> /// /// </summary> /// <param name="userName"></param> /// <param name="oldPassword"></param> /// <param name="newPassword"></param> /// <returns></returns> public bool ChangePassword(string userName, string newPassword) { return(_provider.ChangePassword(userName, null, newPassword)); }