public async Task <ActionResult> Login(LoginViewModel model, string returnUrl) { if (!ModelState.IsValid) { return(View(model)); } // This doesn't count login failures towards account lockout // To enable password failures to trigger account lockout, change to shouldLockout: true UserAuthServiceModel user = new UserAuthServiceModel() { Email = model.Email, Password = model.Password }; user = _commonService.GetLoginUserDetails(user); if (user != null) { FormsAuthentication.SetAuthCookie(model.Email, false); var authTicket = new FormsAuthenticationTicket(1, user.Email, System.DateTime.Now, DateTime.Now.AddMinutes(20), false, user.Roles); Session["CompanyId"] = user.CompanyId == null?0:user.CompanyId; string encryptedTicket = FormsAuthentication.Encrypt(authTicket); var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket); HttpContext.Response.Cookies.Add(authCookie); return(RedirectToAction("DashboardV1", "Home")); } else { ModelState.AddModelError("", "Invalid login attempt."); return(View(model)); } }
public UserAuthServiceModel GetLoginUserDetails(UserAuthServiceModel user) { UserAuthRepositoryModel UserDetails = new UserAuthRepositoryModel(); UserDetails.Email = user.Email; UserDetails.Password = user.Password; var y = _commonRepository.GetLoginUserDetails(UserDetails); if (y == null) { return(null); } UserAuthServiceModel UserAuthDetails = new UserAuthServiceModel(); UserAuthDetails.Email = y.Email; UserAuthDetails.CompanyId = y.CompanyId; UserAuthDetails.Roles = y.Roles; return(UserAuthDetails); }