My ResetPassword does not update on database

88 Views Asked by At

I'm currently working on a Forgot Password and Reset Password on MVC. The program confirms the passwords are equal, if they are empty, if the user exists, etc. Yet, when the user enters a new password, via the link sent on their email, their input does not update in the database. I've tried many other programs, researched how to do it, but I can't seem to find what I am missing to save the changes on the SSMS database.

On my HomeController.cs is

[HttpGet]
    public ActionResult ResetPassword()
    {
        return View();
    }


    
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult ResetPassword(UserLogin user)
    {

        if (ModelState.IsValid) {
            var users = db.UserLogins.Where(a => a.ConfirmPassword.Equals(user.Password)).FirstOrDefault();
            if (user != null)
            {
                //users.Password = user.ConfirmPassword;
                //users.Password = user.Password;
                //users.ConfirmPassword = "";
                //db.Configuration.ValidateOnSaveEnabled = false;

                db.UserLogins.Remove(users);
                var newPassword = user.Password;
                newPassword = encryptPassword(users.Password);
                //users.Password = user.Password;
                //user.Password = users.ConfirmPassword;
                //db.UserLogins.Add(user);
                //encryptPassword(user.Password);
                db.Configuration.ValidateOnSaveEnabled = false;
                db.SaveChanges();
                ViewBag.Message = "Password updated successfully, you may return to homepage";

                RedirectToAction("Login");
            }
            else
            {
                ViewBag.Message = "Something went wrong";
            }
        }
        return View();
    }

When sent the ResetPassword.cshtml via email

@model ProductionDashboardApp.Models.UserLogin

@{
    ViewBag.Title = "ResetPassword";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<body style="text-align:center">
    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)
        <div class="login-page">
            <div class="form">
                <form class="login-form">
                    <div class="editor-label">
                        @Html.LabelFor(model => model.Password, "Contraseña")
                    </div>
                    <div class="editor-field">
                        @Html.PasswordFor(model => model.Password)
                        @Html.ValidationMessageFor(model => model.Password)
                    </div><div class="editor-label">
                        @Html.LabelFor(model => model.ConfirmPassword, "Confirmar contraseña")
                    </div>
                    <div class="editor-field">
                        @Html.PasswordFor(model => model.ConfirmPassword)
                        @Html.ValidationMessageFor(model => model.ConfirmPassword)
                    </div>
                    <input type="submit" value="RESET" />
                </form>
            </div>
        </div>
    }
</body>

And my UserLogin.cs is

    namespace ProductionDashboardApp.Models
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;

    public partial class UserLogin
    {
        [Required(ErrorMessage = "Please, provide your Employee ID", AllowEmptyStrings = false)]

        public int UserID { get; set; }
        [Required(ErrorMessage = "Please, provide your first name", AllowEmptyStrings = false)]

        public string FirstName { get; set; }
        [Required(ErrorMessage = "Please, provide your last name", AllowEmptyStrings = false)]

        public string LastName { get; set; }
        [Required(ErrorMessage = "Please, provide an username", AllowEmptyStrings = false)]

        public string Username { get; set; }


        [Required(ErrorMessage = "Please, provide a password", AllowEmptyStrings = false)]
        [DataType(DataType.Password)]
        public string Password { get; set; }
        [DataType(DataType.Password)]
        [Compare("Password", ErrorMessage = "Passwords do not match")]
        public string ConfirmPassword { get; set; }

        [Required(ErrorMessage = "Please, provide your email", AllowEmptyStrings = false)]
        [DataType(DataType.EmailAddress)]

        public string Email { get; set; }
    }
}
1

There are 1 best solutions below

1
Rahul Sharma On

In your Controller method, you need to Update the entity:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult ResetPassword(UserLogin user)
{

    if (ModelState.IsValid) {
        var users = db.UserLogins.Where(a => a.ConfirmPassword.Equals(user.Password)).FirstOrDefault();
        if (user != null)
        {
            //Generate new password here for the user
            users.Password = encryptPassword(user.Password);
            db.UserLogins.Update(users);
            db.SaveChanges();
            ViewBag.Message = "Password updated successfully, you may return to homepage";

            RedirectToAction("Login");
        }
        else
        {
            ViewBag.Message = "Something went wrong";
        }
    }
    return View();
}