首页 > 解决方案 > 如何在mvc中更新登录密码?

问题描述

我想更新登录密码

当用户登录然后看到他们自己的个人资料后,我在我的应用程序中做什么

学生.cs

namespace Demo.Models
{
    public class student
    {
        public int studentid { get; set; }

        [Display(Name = "FirstName")]
        public string firstname { get; set; }

        [Display(Name = "LastName")]
        public string lastname { get; set; }

        [Display(Name = "UserName")]
        public string username { get; set; }

        [Display(Name = "Password")]
        public string password { get; set; }

        [Display(Name = "Emailid")]
        public string emailid { get; set; }
    }

家庭控制器.cs

        public ActionResult Index()
        {
            if (Session["username"] == null)
            {
                return RedirectToAction("Login", "Home");
            }
            else
            {
                return View(getcurrentstudent());
            }
        }

        public student getcurrentstudent()       
        {
            var currentusername = Session["username"].ToString();
            var currentpassword = Session["password"].ToString();
            var currentstu = _dbcontextstud.stud.Where(s => s.username == currentusername && s.password == currentpassword).SingleOrDefault();

            return currentstu;
        }

        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(student stud)
        {
            var login = _dbcontextstud.stud.Where(x => x.username == stud.username && x.password == stud.password).FirstOrDefault();

            if (login != null)
            {
                Session["username"] = login.username.ToString();
                Session["password"] = login.password.ToString();

                return RedirectToAction("Index");
            }

            return RedirectToAction("Login");
        }

        public ActionResult create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult create(student stud)
        {
            var create = _dbcontextstud.stud.Add(stud);
            _dbcontextstud.SaveChanges();
            return RedirectToAction("Login");
        }

索引.cshtml

@model Demo.Models.student

<h2>Index</h2>

@if (Session["username"] != null)
{
    <text>Welcome <strong>@Session["username"].ToString()</strong></text>
}

<p>

    @Html.ActionLink("Logout", "Logout")

</p>
<table class="table">
    <tr>
        <td>
            @Html.DisplayFor(model => model.firstname)
        </td>
        <td>
            @Html.DisplayFor(model => model.lastname)
        </td>
        <td>
            @Html.DisplayFor(model => model.username)
        </td>
        <td>
            @Html.DisplayFor(model => model.password)
        </td>
        <td>
            @Html.DisplayFor(model => model.emailid)
        </td>
    </tr>
</table>

数据库图:

https://i.stack.imgur.com/mCwSr.png

登录时查看参考:

在此处输入图像描述

如何在 MVC 中更新登录密码或哪些字段额外需要添加表来更新密码或更新密码的逻辑实现?

请帮忙

标签: asp.net-mvc

解决方案


在您的情况下,您只需要使用实体框架更新数据库属性。

  1. 从数据库中获取项目:_dbcontextstud.stud.FirstOrDefault()

  2. 更改项目属性:user.Password = model.NewPassword

  3. 保存更改:_dbcontextstud.SaveChanges()

例子:

[HttpPost]
public ActionResult ChangePassword(ChangePasswordViewModel model)
{
    var currentUsername = Session["username"].ToString();
    var currentPassword = model.CurrentPassword;
    
    var user = _dbcontextstud.stud.FirstOrDefault(s => s.username == currentUsername && s.password == currentPassword);
    if(user == null)
    {
        // user password did not match the DB password
        throw new Exception("Invalid password exception");
    }

    // update the new password
    user.Password = model.NewPassword;
    
    // save the Database changes
    _dbcontextstud.SaveChanges();

    return RedirectToAction("Index");
}

// view model
public class ChangePasswordViewModel
{
    [Required]
    public string CurrentPassword { get; set; }
    
    [Required]
    public string NewPassword { get; set; }
}

推荐阅读