How to make a hash user login (password) in the database, and then read the hashed password during login?
I believe that the solution is to hash the password during registration, where the password is stored as hashed inside db. Later, after logging in, he should not accept and compare his password with the user's password. But I do not know how to do this.
I allowed the password to have nvarchar(MAX)in db, since the hashed password is usually long.
[Required]
[StringLength(MAX, MinimumLength = 3, ErrorMessage = "min 3, max 50 letters")]
public string Password { get; set; }
Registration:
[HttpPost]
public ActionResult Register(User user) {
if (ModelState.IsValid) {
var u = new User {
UserName = user.UserName,
Password = user.Password
};
db.Users.Add(u);
db.SaveChanges();
return RedirectToAction("Login");
}
}return View();
}
Input:
public ActionResult Login() {
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Login(User u) {
if (ModelState.IsValid)
{
using (UserEntities db = new UserEntities()) {
var v = db.Users.Where(a => a.UserName.Equals(u.UserName) && a.Password.Equals(u.Password)).FirstOrDefault();
if (v != null) {
return RedirectToAction("Index", "Home");
}
}
}return View(u);
}
First I use a database.
source
share