public ActionResult Register(UserModel userModel) { using (webdbEntities dbmodel = new webdbEntities()) { if (dbmodel.users.Any(x => x.user_name == userModel.user_name)) { ViewBag.Message = "UserName already exist"; return(View("Register")); } else if (dbmodel.users.Any(x => x.email == userModel.email)) { ViewBag.Message = "Email already exist"; return(View("Register")); } else { user users = new user(); users.created_on = DateTime.Now; users.email = userModel.email; users.user_name = userModel.user_name; users.first_name = userModel.first_name; users.last_name = userModel.last_name; users.password = userModel.password; users.confirm_password = userModel.confirm_password; dbmodel.users.Add(users); dbmodel.SaveChanges(); ModelState.Clear(); ViewBag.Message = "Register success! Now you can log-in"; return(View("Register")); } } }
public ActionResult Login(LoginModel objloginModel) { using (webdbEntities dbmodel = new webdbEntities()) { var check = dbmodel.users.Where(m => m.user_name == objloginModel.user_name && m.password == objloginModel.password).FirstOrDefault(); if (check == null) { ViewBag.Message = "Login failed"; ModelState.AddModelError("Error", "username and pass does not matching"); return(View()); } else { Session["username"] = check.user_name; ViewBag.Message = "Login successfull"; return(RedirectToAction("Index", "Home")); } } }