public ActionResult Register(SignUpViewModel model) { ActionResult resultView = null; try { if (model.IsValid) { var currentUser = _db.GetUser(model.Email); if (currentUser != null) { ViewBag.ErrorMessage = "This username is unavailable"; throw new Exception(); } else { PasswordHashHelper hash = new PasswordHashHelper(model.Password); var newUser = new User { FirstName = model.FirstName, LastName = model.LastName, Email = model.Email, Password = model.Password, HashedPassword = hash.Hash, Salt = hash.Salt, RoleId = model.RoleId }; // Add user to database newUser.UserId = _db.CreateUser(newUser); // Log the user in and redirect to the dashboard LogUserIn(newUser); resultView = RedirectToAction("Dashboard", "Home"); } } else { throw new Exception(); } } catch (Exception) { var selListModel = ConvertListToSelectList(_db.GetRoles()); resultView = View("Register", selListModel); } return(resultView); }
public ActionResult Login(LoginViewModel model) { ActionResult result = null; if (model.IsValid) { var user = _db.GetUser(model.Email); var Hash = ""; if (user != null) { PasswordHashHelper hash = new PasswordHashHelper(model.Password, user.Salt); Hash = hash.Hash; } if (user == null) { ModelState.AddModelError("invalid-user", "The username provided does not exist"); return(View("Login", model)); } else if (user.Password != Hash) { ModelState.AddModelError("invalid-password", "The password provided is not valid"); result = View("Login", model); } else { // adds the user to the session variable using the username key LogUserIn(user); result = RedirectToAction("Dashboard", "Home"); } } else { result = View("Login", model); } return(result); }