public static void ResetCurrentUserSession(string userName = null) { if (string.IsNullOrEmpty(userName)) { userName = System.Web.HttpContext.Current.User.Identity.Name; } using (var db = new MyUtubeEntities()) { var user = db.Users.FirstOrDefault(n => n.Username == userName); if (user == null) { return; } CurrentSession.CurrentUser = user; } }
public ActionResult Login(LoginViewModel model) { //check in the database compare if the model.Username is same in the database using (var db = new MyUtubeEntities()) { var exituser = db.Users.Where(x => x.Username == model.Username && x.Password == model.Password).FirstOrDefault(); if (exituser == null) { ViewBag.IncorrectLogin = "******"; return(View()); } else { // set cookie FormsAuthentication.SetAuthCookie(model.Username, true); // reset currentusersession ResetCurrentUserSession(model.Username); //redirect home index page return(RedirectToAction("Index", "Home")); } } }