一用户
1.1用户注册
1.2用户登录
1.3修改密码
修改密码会用到一个UserChangePassword 模型类,先添加UserChangePassword类
/// <summary> /// 用户修改密码模型 /// </summary> [NotMapped] public class UserChangePassword { /// <summary> /// 原密码 /// </summary> [Display(Name = "原密码")] [Required(ErrorMessage = "×")] [StringLength(20, MinimumLength = 6, ErrorMessage = "×")] [DataType(DataType.Password)] public string Password { get; set; } /// <summary> /// 新密码 /// </summary> [Display(Name = "新密码", Description = "6-20个字符。")] [Required(ErrorMessage = "×")] [StringLength(20, MinimumLength = 6, ErrorMessage = "×")] [DataType(DataType.Password)] public string NewPassword { get; set; } /// <summary> /// 确认密码 /// </summary> [Display(Name = "确认密码", Description = "再次输入密码。")] [Compare("NewPassword", ErrorMessage = "×")] [DataType(DataType.Password)] public string ConfirmPassword { get; set; } }
用到了数据库的更新,先打开UserRepository.cs 修改Update函数
/// <summary> /// 更新用户信息 /// </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; }
在修改密码时需要查出用户信息并更新,需要添加Find(string UserName) 函数
/// <summary> /// 查找用户 /// </summary> /// <param name="UserName">用户名</param> /// <returns></returns> public User Find(string UserName) { return dbContext.Users.SingleOrDefault(u => u.UserName == UserName); }
好了打开UserController,添加ChangePassword Action
/// <summary> /// 修改密码 /// </summary> /// <returns></returns> [UserAuthorize] public ActionResult ChangePassword() { return View(); } [HttpPost] [UserAuthorize] public ActionResult ChangePassword() { return View(); }
添加强类型(UserChangePassword)视图,在视图中添加三个文本框,修改后的视图代码
@model Ninesky.Models.UserChangePassword @{ ViewBag.Title = "修改密码"; Layout = "~/Views/Shared/_Layout.cshtml"; } <div class="banner"> <img src="/UploadFiles/2021-04-02/banner.jpg">修改UserController中[HttpPost]方式的ChangePassword()以实现修改密码,修改完成的代码如下:
[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 = "修改密码失败", Details = "修改密码时,系统查询不到用户信息", Cause = Server.UrlEncode("<li>用户在修改密码界面停留的时间过长,登录信息已失效。</li><li>系统错误。</li>"), Solution = Server.UrlEncode("<li>返回<a href='" + Url.Action("ChangePassword", "User") + "'>修改密码</a>页面,输入正确的信息后重新注册</li><li>联系网站管理员</li>") }; return RedirectToAction("Error", "Prompt", _e); } _user.Password = Common.Text.Sha256(userChangePassword.NewPassword); if (userRsy.Update(_user)) { Notice _n = new Notice { Title = "成功修改密码", Details = "您已经成功修改密码,请牢记您的新密码!", DwellTime = 5, NavigationName = "登陆页面", NavigationUrl = Url.Action("Login", "User") }; return RedirectToAction("Notice", "Prompt", _n); } else { Error _e = new Error { Title = "修改密码失败", Details = "修改密码时,更新数据库失败!", Cause = Server.UrlEncode("<li>系统错误。</li>"), Solution = Server.UrlEncode("<li>返回<a href='" + Url.Action("ChangePassword", "User") + "'>修改密码</a>页面,输入正确的信息后重新注册</li><li>联系网站管理员</li>") }; return RedirectToAction("Error", "Prompt", _e); } } else { ModelState.AddModelError("Password", "原密码不正确,请重新输入"); return View(); } }在上面的代码中用到了一个UserName属性,这个是在UserController添加的属性用于返回Cookie中保存的用户名
/// <summary> /// 获取用户名 /// </summary> public string UserName { get { HttpCookie _cookie = Request.Cookies["User"]; if (_cookie == null) return ""; else return _cookie["UserName"]; } }好了,浏览器中预览一下
测试一下,OK,大功告成!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
标签:
MVC4,网站,用户密码修改
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com
暂无“MVC4制作网站教程第二章 用户密码修改2.3”评论...