0

我使用以下代码进行用户注册。当我调试代码时,密码是加密形式,但它存储为纯文本。密码盐也存储在数据库中。

    [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();
    }

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

4

2 回答 2

2

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

简单的方式(最少的变化)

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();
}
于 2013-08-22T07:47:56.330 回答
0

这是一个很好的例子,可以下载一些代码。

http://www.c-sharpcorner.com/UploadFile/raj1979/how-to-make-custom-login-register-and-logout-in-mvc-4-usin/

于 2014-08-24T10:55:14.510 回答