MVC框架下管理员重置用户密码的实现方案
成功实现管理员重置用户密码功能
经过一番摸索,在ADyson的热心帮助下,我成功搞定了用户密码重置的功能。
在我的系统里:
- 管理员登录后能看到进入用户管理系统的链接,这个系统支持用户列表查看、创建用户、删除用户和修改用户信息
- 任何用户登录后都可以修改自己的密码;但如果用户忘记密码,就得由管理员来重置密码——我没采用发送邮件链接这类复杂方式,而是在管理员界面的「用户列表」区域里,操作列直接加了编辑、删除、查看详情和重置密码的链接
我有一个包含编辑、删除等功能的ApplicationUsersController,还有Create、Edit、Delete、Details、Index等一系列自动生成的ApplicationUsers视图。另外专门做了ResetUserPasswordViewModel,下面是具体的代码实现:
ResetPassword视图代码
@model ICWeb.Models.ResetUserPasswordViewModel @{ ViewBag.Title = "Reset User Password"; Layout = "~/Views/Shared/_Layout.cshtml"; } <h2>@ViewBag.Title</h2> @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div class="form-horizontal"> <hr /> @Html.ValidationSummary(true, "Please fix the errors displayed", new { @class = "text-danger" }) @Html.HiddenFor(model => model.Id) <div class="form-group"> @Html.LabelFor(model => model.NewPassword, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.NewPassword, new { htmlAttributes = new { @class = "form-control", @autofocus = "autofocus" } }) @Html.ValidationMessageFor(model => model.NewPassword, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.ConfirmPassword, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.ConfirmPassword, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.ConfirmPassword, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="Reset Password" class="btn btn-default" /> </div> </div> </div> } <div> @Html.ActionLink("Back to List", "Index") </div> @section Scripts { @Scripts.Render("~/bundles/jqueryval") }
控制器中的ResetPassword方法代码
// GET: /ApplicationUsers/ResetPassword public ActionResult ResetPassword(string id) { return View(new ResetUserPasswordViewModel() { Id = id }); } //POST: /ApplicationUsers/ResetPassword [HttpPost] [ValidateAntiForgeryToken] public async Task<ActionResult> ResetPassword(ResetUserPasswordViewModel model) { if (!ModelState.IsValid) { return View(model); } ApplicationDbContext context = new ApplicationDbContext(); UserStore<ApplicationUser> store = new UserStore<ApplicationUser>(context); UserManager<ApplicationUser> UserManager = new UserManager<ApplicationUser>(store); string userId = model.Id; string newPassword = model.NewPassword; string hashedNewPassword = UserManager.PasswordHasher.HashPassword(newPassword); ApplicationUser cUser = await store.FindByIdAsync(userId); await store.SetPasswordHashAsync(cUser, hashedNewPassword); await store.UpdateAsync(cUser); return RedirectToAction("Index"); }
我后来重新编写了这个方法,现在视图能正常加载,输入两个新密码提交后,ResetPassword函数会正常执行。调试时能看到输入的密码,通过修改GET方法填充模型的Id,现在也能正确获取到用户ID。而且这个控制器只对管理员开放权限,非管理员是访问不了的。
ResetUserPasswordViewModel代码
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.ComponentModel.DataAnnotations; namespace ICWeb.Models { public class ResetUserPasswordViewModel { public string Id { get; set; } [Required] [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)] [DataType(DataType.Password)] [Display(Name = "New password")] public string NewPassword { get; set; } [DataType(DataType.Password)] [Display(Name = "Confirm new password")] [Compare("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")] public string ConfirmPassword { get; set; } } }
现在所有问题都解决啦,真的非常感谢帮忙!
内容的提问来源于stack exchange,提问作者snert
相关产品推荐
相关产品推荐

