学用MVC4做网站一:修改密码1.3
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="~/Skins/Default/Images/banner.jpg" /> </div> @using (Html.BeginForm()) { @Html.ValidationSummary(true) <div class="form"> <dl> <dt>修改密码</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> <dd> <div class="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> <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> <dd> <div class="label"></div> <div class="ctrl"> <input type="submit" value="修改密码" />@Html.ValidationMessage("Message") </div> </dd> </dl> <div class="clear"></div> </div> } @section Scripts { @Scripts.Render("~/bundles/jqueryval") }
修改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,大功告成!