ASP.NET Core 打造一个简单的图书馆管理系统(四)密码修改以及密码重置

  前言:

  本系列文章主要为我之前所学知识的一次微小的实践,以我学校图书馆管理系统为雏形所作。

  本系列文章主要参考资料:

  微软文档:upload/201812241655521581.gif" alt="复制代码" style="margin: 0px; padding: 0px; max-width: 900px; height: auto; border: none !important;" />

 1     public class ModifyModel 2     { 3         [UIHint("password")] 4         [Display(Name = "原密码")] 5         [Required] 6         public string OriginalPassword { get; set; } 7  8         [Required] 9         [Display(Name = "新密码")]10         [UIHint("password")]11         public string ModifiedPassword { get; set; }12 13         [Required]14         [Display(Name = "确认密码")]15         [UIHint("password")]16         [Compare("ModifiedPassword", ErrorMessage = "两次密码不匹配")]17         public string ConfirmedPassword { get; set; }18     }
复制代码

 

  在 StudentAccountController 中添加 [Authorize] 特性,然后可以去除 StudentAccountController 中方法的 [Authorize] 特性。当方法不需要授权即可访问时添加 [AllowAnonymous] 特性。

复制代码
1     [Authorize]2     public class StudentAccountController : Controller
复制代码

 

  利用 Identity 框架中 UserManager 对象的 ChangePasswordAsync 方法用来修改密码,该方法返回一个 IdentityResult 对象,可通过其 Succeeded 属性查看操作是否成功。在此修改成功后调用 _signInManager.SignOutAsync() 方法来清除当前 Cookie。

  定义用于修改密码的动作方法和视图:

复制代码
 1         public IActionResult ModifyPassword() 2         { 3             ModifyModel model=new ModifyModel(); 4             return View(model); 5         } 6  7         [HttpPost] 8         [ValidateAntiForgeryToken] 9         public async Task<IActionResult> ModifyPassword(ModifyModel model)10         {11             if (ModelState.IsValid)12             {13                 string username = HttpContext.User.Identity.Name;14                 var student = _userManager.Users.FirstOrDefault(s => s.UserName == username);15                 var result =16                     await _userManager.ChangePasswordAsync(student, model.OriginalPassword, model.ModifiedPassword);17                 if (result.Succeeded)18                 {19                     await _signInManager.SignOutAsync();20                     return View("ModifySuccess");21                 }22                 ModelState.AddModelError("","原密码输入错误");23             }24             return View(model);25         }
复制代码

 

   ModifyPassword 视图,添加用以表示是否显示密码的复选框,并使用 jQuery 和 JS 添加相应的事件。将<script></script>标签统一放在 @section Scripts 以方便地使用布局:

复制代码
 1     @model ModifyModel 2  3     @{ 4         ViewData["Title"] = "ModifyPassword"; 5     } 6  7     @section Scripts{  8         <script> 9             $(document).ready(function() {10                 var $btn = $("#showPas");11                 var btn = $btn.get(0);12                 $btn.click(function() {13                     if (btn.checked) {14                         $(".pass").attr("type", "");15                     } else {16                         $(".pass").attr("type", "password");17                     }18                 });19             })20         </script>21     }22 23 24     <h2>修改密码</h2>25 26     <div class="text-danger" asp-validation-summary="All"></div>27     <form asp-action="ModifyPassword" method="post">28         <div class="form-group">29             <label asp-for="OriginalPassword"></label>30             <input asp-for="OriginalPassword" class="pass"/>31         </div>32         <div class="form-group">33             <label asp-for="ModifiedPassword"></label>34             <input asp-for="ModifiedPassword" id="modifiedPassword" class="pass"/>35         </div>36         <div class="form-group">37             <label asp-for="ConfirmedPassword"></label>38             <input asp-for="ConfirmedPassword" id="confirmedPassword" onkeydown="" class="pass"/>39         </div>40         <div class="form-group">41             <label>显示密码 </label><input style="margin-left: 10px" type="checkbox" id="showPas"/>42         </div>43         <input type="submit"/>44         <input type="reset"/>45     </form>
复制代码

 

   随便建的 ModifySuccess 视图:

复制代码
1     @{2         ViewData["Title"] = "修改成功";3     }4 5     <h2>修改成功</h2>6 7     <h4><a asp-action="Login">请重新登录</a></h4>
复制代码

 

   然后修改 AccountIn

50000+
5万行代码练就真实本领
17年
创办于2008年老牌培训机构
1000+
合作企业
98%
就业率

联系我们

电话咨询

0532-85025005

扫码添加微信