Wasfa
Wasfa

Reputation: 266

Encrypted password is not saving in database but only salt password

I have used the following code for user registeration.When i debugged the code password is in encrypted form but it is stored as plain text.Password salt has been also stored in 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();
    }

Any help will be appreciated.Thanks!

Upvotes: 0

Views: 1120

Answers (2)

thegunner
thegunner

Reputation: 7153

This is a good example with some code to download.

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

Upvotes: 0

You are not storing register in the database, you are storing the original item(reg).

easiset way(least changes would be)

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

Upvotes: 2

Related Questions