MVC4 website creation tutorial Chapter 2 User Password modification 2.3 and mvc4 User Password Modification
One user
1.1 User Registration
1.2 User Logon
1.3 Change Password
A UserChangePassword model class is used to change the password. First, add the UserChangePassword class.
/// <Summary> // modify the password model /// </summary> [NotMapped] public class UserChangePassword {// <summary> // The original password // /</summary> [Display (Name = "original password")] [Required (ErrorMessage = "×")] [StringLength (20, MinimumLength = 6, ErrorMessage = "×")] [DataType (DataType. password)] public string Password {get; set ;}/// <summary> /// new Password /// </summary> [Display (Name = "new Password ", description = "6-20 characters. ")] [Required (ErrorMessage =" × ")] [StringLength (20, MinimumLength = 6, ErrorMessage =" × ")] [DataType (DataType. password)] public string NewPassword {get; set ;}/// <summary> /// confirm the Password /// </summary> [Display (Name = "Confirm Password ", description = "enter the password again. ")] [Compare (" NewPassword ", ErrorMessage =" × ")] [DataType (DataType. Password)] public string ConfirmPassword {get; set ;}}
When database updates are used, enable UserRepository. cs to modify the Update function.
/// <Summary> /// update user information /// </summary> /// <param name = "user"> </param> /// <returns> </returns> public override bool Update (User user) {dbContext. users. attach (user); dbContext. entry <User> (user ). state = System. data. entityState. modified; if (dbContext. saveChanges ()> 0) return true; else return false ;}
When changing the password, you need to Find the user information and update it. You need to add the Find (string UserName) function.
/// <Summary> /// find the user /// </summary> /// <param name = "UserName"> User name </param> /// <returns> </returns> public User Find (string UserName) {return dbContext. users. singleOrDefault (u => u. userName = UserName );}
Okay. Open UserController and add ChangePassword Action.
/// <Summary> /// change the password /// </summary> /// <returns> </returns> [UserAuthorize] public ActionResult ChangePassword () {return View ();} [HttpPost] [UserAuthorize] public ActionResult ChangePassword () {return View ();}
Add the UserChangePassword view, add three text boxes to the view, and modify the view code.
@ Model Ninesky. Models. UserChangePassword @ {ViewBag. Title = "Change Password"; Layout = "~ /Views/Shared/_ Layout. cshtml ";} <div class =" banner "> </div> @ using (Html. beginForm () {@ Html. validationSummary (true) <div class = "form"> <dl> <dt> Change Password </dt> <dd> <div class = "label"> @ Html. labelFor (model => model. password): </div> <div class = "ctrl"> @ Html. passwordFor (model => model. password) @ Html. validationMessageFor (model => model. password) @ Html. displayDescriptionFor (model => model. password) </div> </dd> <div clas S = "label"> @ Html. labelFor (model => model. newPassword): </div> <div class = "ctrl"> @ Html. passwordFor (model => model. newPassword) @ Html. validationMessageFor (model => model. newPassword) @ Html. displayDescriptionFor (model => model. newPassword) </div> </dd> <div class = "label"> @ Html. labelFor (model => model. confirmPassword): </div> <div class = "ctrl"> @ Html. passwordFor (model => model. confirmPassword) @ Html. ValidationMessageFor (model => model. confirmPassword) @ Html. displayDescriptionFor (model => model. confirmPassword) </div> </dd> <div class = "label"> </div> <div class = "ctrl"> <input type = "submit" value = "Change Password"/> @ Html. validationMessage ("Message ") </div> </dd> </dl> <div class = "clear"> </div>} @ section Scripts {@ Scripts. render ("~ /Bundles/jqueryval ")}
Modify ChangePassword () in the [HttpPost] mode in UserController to change the password. The code after the modification is as follows:
[HttpPost] [UserAuthorize] public ActionResult ChangePassword (UserChangePassword userChangePassword) {userRsy = new UserRepository (); if (userRsy. authentication (UserName, Common. text. sha256 (userChangePassword. password) = 0) {var _ user = userRsy. find (UserName); if (_ user = null) {Error _ e = new Error {Title = "failed to change the password", Details = "when changing the password, the system cannot query user information ", Cause = Server. urlEncode ("<li> time the user stays on the password modification page The logon information is invalid because it is too long. </Li> <li> system error. </Li> "), Solution = Server. urlEncode ("<li> Returns <a href = '" + Url. action ("ChangePassword", "User") + "'> Change Password </a> page, enter the correct information and register again </li> <li> contact the website administrator </li> ")}; return RedirectToAction (" Error "," Prompt ", _ e);} _ user. password = Common. text. sha256 (userChangePassword. newPassword); if (userRsy. update (_ user) {Notice _ n = new Notice {Title = "successfully changed password", Details = "you have successfully changed the password. Please remember your new password! ", DwellTime = 5, NavigationName =" Logon page ", NavigationUrl = Url. action ("Login", "User")}; return RedirectToAction ("Notice", "Prompt", _ n );} else {Error _ e = new Error {Title = "failed to Change Password", Details = "failed to update database when changing password! ", Cause = Server. UrlEncode (" <li> system error. </Li> "), Solution = Server. urlEncode ("<li> Returns <a href = '" + Url. action ("ChangePassword", "User") + "'> Change Password </a> page, enter the correct information and register again </li> <li> contact the website administrator </li> ")}; return RedirectToAction (" Error "," Prompt ", _ e) ;}} else {ModelState. addModelError ("Password", "the original Password is incorrect. Please enter it again"); return View ();}}
In the above Code, a UserName attribute is used. This attribute added in UserController is used to return the user name saved in the Cookie.
/// <Summary> /// obtain the UserName /// </summary> public string UserName {get {HttpCookie _ cookie = Request. cookies ["User"]; if (_ cookie = null) return ""; else return _ cookie ["UserName"] ;}}
Okay, preview it in the browser.
Test it. OK. All done!
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.