【问题标题】:How can i check if the password i add to the textbox, is same as the password in database using mvc如何检查我添加到文本框中的密码是否与使用 mvc 的数据库中的密码相同
【发布时间】:2019-07-08 16:08:35
【问题描述】:

我有一个登录表单,当您单击登录按钮时,会显示所有用户的数据。当数据显示在表单字段中时,我有一个旧密码、新密码和确认密码字段,用户可以在其中更改密码。现在,当用户输入他的旧密码时,我想检查它是否与存储在数据库中的密码相同。

这就是我从数据库中获取数据的方式:

    [HttpGet]
    public ActionResult Edit(int? id, UpdateModel updatemodel)
    {
        id = 1;
        SqlConnection cn = new SqlConnection("@")

        SqlCommand cmd1 = new SqlCommand(
           "Select Username , Password From BS_Users Where IDBS_Persons=" + id, cn);

        cn.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        SqlDataReader dr1 = cmd1.ExecuteReader();
        if (dr1.Read())
        { 
            updatemodel.Username = dr1["Username"].ToString();
            updatemodel.Password = dr1["Password"].ToString();
        }
        else
        {
            dr.Close();
        }
        dr.Close();
        cn.Close();
        return View(updatemodel);
    }

这就是我发布它们的方式:

    [HttpPost]
    public ActionResult Edit(UpdateModel p, FormCollection form, int? id)
    {
        id = 1;
        SmartFinanceContext db = new SmartFinanceContext();
        if (ModelState.IsValid)
        {          
            int users = p.UpdateUser(p.Username, Crypto.Hash(p.Password), id = 1);  
            if (_records > 0 && users > 0)
            {
                db.SaveChanges();
                return RedirectToAction("Edit", "Home");
            }

            {
                ModelState.AddModelError("", "Can Not Update");
            }
        }
        return View(p);
    }

【问题讨论】:

  • 会发生什么?
  • 当它检查密码是否相同时,它应该让用户在“新密码”字段中更改密码

标签: sql asp.net database model-view-controller


【解决方案1】:

应该是这样的:

//Get from database, which would be an encrypted string
var pw = dr1["Password"].ToString();
//Encrypt password sent in by user
var pwCheck = Crypto.Hash(p.Password)

//Check encrypted password from database against encrypted passed-in password
if (pw == pwCheck){
    //do something
}

【讨论】:

    猜你喜欢
    • 2016-01-06
    • 2020-11-27
    • 2020-09-05
    • 2020-07-03
    • 1970-01-01
    • 2018-04-16
    • 1970-01-01
    • 2019-04-30
    • 2017-01-17
    相关资源
    最近更新 更多