How to change password in asp.net mvc


i want to change the current password using asp.net mvc using entity framework.


Asked by:- rahmat
0
: 15019 At:- 9/28/2017 11:11:22 AM
changing password in asp.net mvc asp.net mvc

if possible, when asking a question, always add your current code or images, so that other users get a better idea of your issue & current code, thanks 0
By : vikas_jk - at :- 9/28/2017 3:09:50 PM
your question doesn't have enough details to answer, please edit it to provide more details? Did you tried using the MVC template (while creating Project, select MVC template instead of empty one), template provides all the code to change the password, user confirmation etc. 0
By : jaya - at :- 10/15/2017 8:41:53 AM






3 Answers
profileImage Answered by:- vikas_jk

Using Asp.Net Identity, you can reset password(change password ) with the following C# code in your controller

  // Get View using this Method
        public ActionResult ChangePassword()
        {
            return View();
        }

        //
        //Save Details of New Password using Identity
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> ChangePassword(ChangePasswordViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }
            var result = await UserManager.ChangePasswordAsync(User.Identity.GetUserId<int>(), model.OldPassword, model.NewPassword);
            if (result.Succeeded)
            {
                var user = await UserManager.FindByIdAsync(User.Identity.GetUserId<int>());
                if (user != null)
                {
                    await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
                }
                return RedirectToAction("Index", new { Message = ManageMessageId.ChangePasswordSuccess });
            }
            AddErrors(result);
            return View(model);
        }

and in the View, your code can be

@model YourProject.Models.ChangePasswordViewModel
@{
    ViewBag.Title = "Change Password";
}

<h1>@ViewBag.Title</h1>

@using (Html.BeginForm("ChangePassword", "ControllerName", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
    @Html.AntiForgeryToken()  
    
    @Html.ValidationSummary("", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(m => m.OldPassword, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.PasswordFor(m => m.OldPassword, new { @class = "form-control" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.NewPassword, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.PasswordFor(m => m.NewPassword, new { @class = "form-control" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.ConfirmPassword, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" })
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Change password" class="btn btn-primary" />
        </div>
    </div>
}
@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

 ChangePasswordViewModel.cs code looks like this

 public class ChangePasswordViewModel
    {
        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "Current password")]
        public string OldPassword { 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; }
    }

The above code is what you will get if you will create asp.net MVC project with the Basic template instead of "Empty" template, as this template auto-generate these code.

If you are not using  ASP.NET identity, you can add it in your Project or try looking at forgot password functionality in mvc article

You can use it with Entity Framework.

1
At:- 9/28/2017 1:57:51 PM


profileImage Answered by:- jaya

Above answer should work for you, but as your details of the question is not enough & it may not work, then take a look at these url's for help

https://stackoverflow.com/questions/33988925/mvc-net-password-change

https://stackoverflow.com/questions/34481480/how-to-change-users-password-using-mvc-controller-with-views-using-entity-framew

Reset Password : http://foxlearn.com/article/implement-password-reset-with-asp-net-identity-mvc-37.html

Hope this helps, if not please provide better details on your questions

0
At:- 10/15/2017 8:46:59 AM


profileImage Answered by:- neena

Without Identity to change password, you can follow this procedure

Create a Model to change the password

public class UserModel{
   public string Email{get;set;}
   public string Password{get;set;}
   public string NewPassword{get;set;}
}

In the View, Create a form with above Model as reference

@using (Html.BeginForm("Changepassword", "Home", FormMethod.Post))
    {
           <table class="center">
             <tr>
                   <td>Email</td>
                   <td>
                       @Html.EditorFor(pass => pass.Email)
                   </td>
                   <td>@Html.ValidationMessageFor(pass => pass.Email)</td>
               </tr>
               <tr>
                   <td>Old Password</td>
                   <td>
                       @Html.EditorFor(pass => pass.Password)
                   </td>
                   <td>@Html.ValidationMessageFor(pass => pass.Password)</td>
               </tr>
               <tr class="rowspace">
                   <td>New Password</td>

                   <td>
                       @Html.EditorFor(pass => pass.NewPassword)
                   </td>
                   <td>@Html.ValidationMessageFor(pass => pass.NewPassword)</td>
               </tr>

               <tr class="rowspace">
                   <td colspan="3" id="button">
                       <input type="submit" value="Update Password" /></td>
               </tr>
               <tr class="rowspace"><td colspan="3">@ViewBag.Message</td></tr>
           </table>

    }

Now in the C# controller, get form, check if old password matches with what user has entered and then save new password

public ActionResult Changepassword(UserModel login)
    {
       using (UserDetailsEntities db = new UserDetailsEntities())
       {
           var detail = db.tblUsers.Where(log => log.Password == login.Password 
           && log.email == login.email).FirstOrDefault();
           if (detail != null)
           {
                   userDetail.Password = login.NewPassword;

                   db.SaveChanges();
                   ViewBag.Message = "Password updated Successfully!";

              }
     else
               {
                   ViewBag.Message = "Password not Updated!";
               }


       }

       return View(login);
    }

That's it, thanks.

0
At:- 6/28/2021 2:10:56 PM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use