public ActionResult Login(User user) { if (ModelState.IsValid) { User userMatch = db.Users.FirstOrDefault(u => u.Username == user.Username); if (userMatch != null) { if (user.IsPasswordMatch(userMatch)) { FormsAuthentication.SetAuthCookie(user.Username, false); return RedirectToAction("Index", "Articles"); } else { ModelState.AddModelError("", "Incorrect login."); } } else { ModelState.AddModelError("", "User does not exist."); } } return View(user); }
public ActionResult Register(User user) { if (ModelState.IsValid) { User userMatch = db.Users.FirstOrDefault(u => u.Username == user.Username); if (userMatch == null) { PasswordManager pm = new PasswordManager(user.Username, user.Hash); user.Salt = pm.salt.getSaltString(); db.Users.Add(user); db.SaveChanges(); TempData["message"] = "Successfully added User."; return RedirectToAction("Login", "Home"); } else { ModelState.AddModelError("", "That Username is already taken!"); } } return View(user); }
public bool IsPasswordMatch(User userToCompare) { // Get password byte array byte[] passwordBytes = Encoding.UTF8.GetBytes(Hash); // Get salt byte array Salt salt = new Salt(); salt.saltString = userToCompare.Salt; byte[] saltBytes = salt.getSaltBytes(); // Compute hash byte array from password and salt Hash hash = new Hash(); hash.computeHashBytes(passwordBytes, saltBytes); string hashString = hash.getHashString(); // Compare the two if (userToCompare.Hash == hashString) { return true; } return false; }