public IActionResult Register(RegisterVM model) { if (!ModelState.IsValid) { RedirectToAction("Register", "Home"); } UsersRepository usersRepo = new UsersRepository(); User user = usersRepo.GetFirstOrDefault(u => u.Username == model.Username); if (user != null) { ModelState.AddModelError("RegisterFailed", "Username already taken"); return(View(model)); } if (model.Password != model.ConfirmPassword) { ModelState.AddModelError("RegisterFailed", "Passwords must match"); return(View(model)); } RolesRepository rolesRepo = new RolesRepository(); Role role = rolesRepo.GetFirstOrDefault(r => r.Name == "User"); user = new User(model.Username, model.Password, role.Id); usersRepo.Insert(user); this.HttpContext.Session.SetString("LoggedUsername", user.Username.ToString()); this.HttpContext.Session.SetString("LoggedUserRole", role.Name); this.HttpContext.Session.SetString("LoggedUserId", user.Id.ToString()); return(RedirectToAction("Index", "Home")); }