6437total visits.
I was wondering if there is a way to reset password with UserManager of ASP.NET MVC 5 some days ago and finally I got simple and effective solution
Resetting users password is one of the features that we need in our application. Well, there are many solutions that you can apply but the most simple and effective would be as mentioned below.
userManager.ResetPassword(UserID) : Resets a user’s password.
userManager.AddPassword(UserId, “New_Password”) : Assigns new password to User.
Step I: let’s Create a ViewModel for resetting password for User as ResetUserPasswordViewModel as

Code Snippet
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
public class ResetUserPasswordViewModel { [Display(Name = "Username")] public string Username { get; set; } [Required] [Display(Name = "User ID")] public string UserId { get; set; } [Required] [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)] [DataType(DataType.Password)] [Display(Name = "Password")] public string Password { get; set; } [DataType(DataType.Password)] [Display(Name = "Confirm password")] [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")] public string ConfirmPassword { get; set; } public string Code { get; set; } } |
Step 2: Add a method called ResetUserPassword accepting Parameter UserId as
|
1 2 3 4 5 6 |
public ActionResult ResetUserPassword(string userId, string UserName) { ViewBag.Username = UserName.ToString(); ViewBag.UserId = userId.Trim().ToString(); return View(); } |
Step 3: Create View with ResetUserPasswordViewModel
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
@model MVC5Demo.UI.ViewModels.ResetUserPasswordViewModel @{ ViewBag.Title = "Reset User Password"; Layout = "~/Views/Shared/_Layout.cshtml"; } <div class="row"> <div class="col-lg-offset-2 col-md-8"> <br /> <div class="" style="background-color: #ffffff; padding: 20px; border-radius: 20px; box-shadow: 0px 0px 5px;"> @using (Html.BeginForm("ResetUserPassword", "ManageUsers", FormMethod.Post, new { @class = "form-horizontal", role = "form", area = "" })) { @Html.AntiForgeryToken() <h4>Reset Password </h4> <hr /> <div class="form-group"> @Html.LabelFor(m => m.Username, new { @class = "col-md-2 control-label" }) <div class="col-md-10"> @Html.TextBoxFor(m => m.Username, new { @class = "form-control", value = ViewBag.Username, @readonly = "readonly" }) </div> </div> <div class="form-group"> @Html.LabelFor(m => m.UserId, new { @class = "col-md-2 control-label" }) <div class="col-md-10"> @Html.TextBoxFor(m => m.UserId, new { @class = "form-control", value = ViewBag.UserId, @readonly = "readonly" }) </div> </div> <div class="form-group"> @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" }) <div class="col-md-10"> @Html.TextBoxFor(m => m.Password, new { @class = "form-control", placeholder = "New Password" }) </div> </div> <div class="form-group"> @Html.LabelFor(m => m.ConfirmPassword, new { @class = "col-md-2 control-label" }) <div class="col-md-10"> @Html.TextBoxFor(m => m.ConfirmPassword, new { @class = "form-control", placeholder = "Confirm Password" }) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" class="btn btn-success" value="Reset Password" /> </div> </div> } </div> </div> </div> |
Step 4: Add New method called ResetUserPassword like
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
[HttpPost] public ActionResult ResetUserPassword(ResetUserPasswordViewModel model) { if (ModelState.IsValid) { UserManager<IdentityUser> userManager = new UserManager<IdentityUser>(new UserStore<IdentityUser>()); if (userManager.HasPassword(model.UserId)) { userManager.RemovePassword(model.UserId); userManager.AddPassword(model.UserId, model.ConfirmPassword); } TempData["Message"] = "Password successfully reset to " + model.ConfirmPassword; TempData["MessageValue"] = "1"; return RedirectToAction("UsersWithRoles", "ManageUsers", new { area = "", }); } // If we got this far, something failed, redisplay form TempData["Message"] = "Invalid User Details. Please try again in some minutes "; TempData["MessageValue"] = "0"; return RedirectToAction("UsersWithRoles", "ManageUsers", new { area = "", }); } |
Note: This method should be of type [HTTPOST] as HTTPPostAtttibute
Now let’s add an action button in UserWithRoles view to enable ResetUser Password functionality in ASP.NET Identity. Note, we have already created ListUser method returning the list of Users with Roles in our Previous Example. [http://aryalnishan.com.np/asp-net-mvc/list-all-users-with-associated-roles-in-asp-net-identity]
UserWithRoles view after adding Reset User Password Button looks like following.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
@model IEnumerable<MVC5Demo.UI.ViewModels.Users_in_Role_ViewModel> @{ ViewBag.Title = "Users With Roles"; Layout = "~/Views/Shared/_Layout.cshtml"; } <div class="panel panel-primary"> <div class="panel-heading"> <h3 class="box-title"> <b>Users with Roles</b> </h3> </div> <!-- /.box-header --> <div class="panel-body"> <table class="table table-hover table-bordered table-condensed" id="example1"> <thead> <tr> <td><b>Username</b></td> <td><b>Email</b></td> <td><b>Roles</b></td> <td><b>Options</b></td> </tr> </thead> @foreach (var user in Model) { <tr> <td>@user.Username</td> <td>@user.Email</td> <td>@user.Role</td> <td class="form-actions"> <span style="color: #ff0000;"><a href="@Url.Action("ResetUserPassword", "ManageUsers", new { userId = user.UserId, UserName = user.Username})" class="btn btn-warning btn-sm"> <i class="fa fa-unlock"> </i>Reset Password </a></span> </td> </tr> } </table> </div> <div class="panel-footer"> <p class="box-title"><b>Total Users till @String.Format("{0 : dddd, MMMM d, yyyy}", DateTime.Now) : </b><span class="label label-primary">@Model.Count()</span></p> </div> </div> @section scripts{ <script> $(function () { $("#example1").DataTable(); $('#example2').DataTable({ "paging": true, "lengthChange": true, "searching": true, "ordering": true, "info": true, "autoWidth": true }); }); </script> } |
Running Application.
Now let’s run the solution after rebuilding and navigate to /ManageUsers/UserWithRoles 
Form when submitted after providing New Password, will reset password to new password of the respective user.
You can download Full Working Project from https://github.com/nishanaryal/ASP.NET-MVC