Get the App
SLTechnology News&Howtos  ›  Development  › 

How to develop user password Modification in MVC4 website

Shulou Source: shulou.com Published: 2022-06-03 12:56:28 11月14日 Update

This article will explain in detail how to develop user password modification in the MVC4 production website, the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

One user

1.1 user Registration

1.2 user login

1.3 change password

A UserChangePassword model class will be used to change the password. Add the UserChangePassword class first.

/ user modifies password model / / [NotMapped] public class UserChangePassword {/ original password / [Display (Name = "original password")] [Required (ErrorMessage = "×")] [StringLength (20, MinimumLength = 6, ErrorMessage = "×")] [DataType (DataType.Password)] public string Password {get; set } / New password / [Display (Name = "New password", Description = "6-20 characters.")] [Required (ErrorMessage = "×")] [StringLength (20, MinimumLength = 6, ErrorMessage = "×")] [DataType (DataType.Password)] public string NewPassword {get; set;} / confirm password / [Display (Name = "confirm password", Description = "enter password again.")] [Compare ("NewPassword", ErrorMessage = "×")] [DataType (DataType.Password)] public string ConfirmPassword {get; set;}}

To update the database, open UserRepository.cs to modify the Update function first.

/ / update user information / public override bool Update (User user) {dbContext.Users.Attach (user); dbContext.Entry (user). State = System.Data.EntityState.Modified; if (dbContext.SaveChanges () > 0) return true; else return false;}

When changing the password, you need to find out the user information and update it, and you need to add the Find (string UserName) function.

/ find the user / username / public User Find (string UserName) {return dbContext.Users.SingleOrDefault (u = > u.UserName = = UserName);}

All right, open UserController and add ChangePassword Action.

/ change password / [UserAuthorize] public ActionResult ChangePassword () {return View ();} [HttpPost] [UserAuthorize] public ActionResult ChangePassword () {return View ();}

Add an enhanced type (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";}

@ using (Html.BeginForm ()) {@ Html.ValidationSummary (true) modify the password @ Html.LabelFor (model = > model.Password): @ Html.PasswordFor (model = > model.Password) @ Html.ValidationMessageFor (model = > model.Password) @ Html.DisplayDescriptionFor (model = > model.Password) @ Html.LabelFor (model = > model.NewPassword): Html.PasswordFor (model = > model.NewPassword) @ Html.ValidationMessageFor (model = > model.NewPassword) @ Html.DisplayDescriptionFor (model = > model.NewPassword) @ Html.LabelFor (model = > model.ConfirmPassword): @ Html.PasswordFor (model = > model.ConfirmPassword) @ Html.ValidationMessageFor (model = > model.ConfirmPassword) @ Html.DisplayDescriptionFor (model = > model.ConfirmPassword) @ Html.ValidationMessage ("Message")} @ section Scripts {@ Scripts.Render ("~ / bundles/jqueryval")}

Modify the ChangePassword () of [HttpPost] mode in UserController to change the password. The modified code 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 = "the system cannot query the user information when changing the password", Cause = Server.UrlEncode ("the user stays in the password modification interface for too long and the login information is invalid. System error. ") , Solution = Server.UrlEncode ("return to the password change page, enter the correct information and re-register with the webmaster")}; return RedirectToAction ("Error", "Prompt", _ e);} _ user.Password = Common.Text.Sha256 (userChangePassword.NewPassword) If (userRsy.Update (_ user)) {Notice _ n = new Notice {Title = "password changed successfully", Details = "you have successfully changed your password, please remember your new password!" , DwellTime = 5, NavigationName = "login 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 while changing password!" , Cause = Server.UrlEncode ("system error.") , Solution = Server.UrlEncode ("return to the change password page, enter the correct information and re-register with the webmaster")}; return RedirectToAction ("Error", "Prompt", _ e);}} else {ModelState.AddModelError ("Password", "incorrect password, please re-enter"); return View ();}}

A UserName property is used in the above code, which is added in UserController to return the user name saved in Cookie

/ get user name / public string UserName {get {HttpCookie _ cookie = Request.Cookies ["User"]; if (_ cookie = = null) return ""; else return _ cookie ["UserName"];}}

All right, preview it in the browser

Test it, OK, and it's done!

On the MVC4 production site on how to develop user password modification to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Password user information website update input code user name system view page new secret making website production development success content function property data Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Redmi Huawei Xiaomi OPPO Reno