加密密码不保存在数据库中,只保存盐密码

时间:2013-08-22 07:41:58

标签: asp.net-mvc-4

我已经使用以下代码进行用户注册。当我调试代码时,密码是加密形式的,但它以纯文本形式存储.Password salt也存储在db中。

    [HttpPost, RecaptchaControlMvc.CaptchaValidator]
    public ActionResult Register(Authentication reg, bool captchaValid, string captchaErrorMessage)
    {
        if (!captchaValid)
            ModelState.AddModelError("captcha", captchaErrorMessage);

        if (ModelState.IsValid)
        {
            Registeration register = new Registeration();

            var crypto = new SimpleCrypto.PBKDF2();

            //string encrypPass = crypto.Compute(reg.Password);
            //var length = encrypPass.Length;

            register.FirstName = reg.FirstName;
            register.LastName = reg.LastName;
            register.Email = reg.Email;
            register.Password = crypto.Compute(reg.Password);
            reg.PasswordSalt = crypto.Salt;

            db.Authentications.InsertOnSubmit(reg);
            db.SubmitChanges();
        }

        return View();
    }

任何帮助都将不胜感激。谢谢!

2 个答案:

答案 0 :(得分:2)

您没有在数据库中存储寄存器,而是存储原始项目(reg)。

easiset方式(最少改变)

public ActionResult Register(Authentication reg, bool captchaValid, string captchaErrorMessage)
{
    if (!captchaValid)
        ModelState.AddModelError("captcha", captchaErrorMessage);

    if (ModelState.IsValid)
    {
        var crypto = new SimpleCrypto.PBKDF2();
        reg.Password = crypto.Compute(reg.Password);
        reg.PasswordSalt = crypto.Salt;

        db.Authentications.InsertOnSubmit(reg);
        db.SubmitChanges();
    }

    return View();
}

答案 1 :(得分:0)