public BusinessResults <User> Login(LoginViewModel data) { string depassword = ""; GeneralHelper generalHelper = new GeneralHelper(); depassword = generalHelper.Encrypt(data.Password); BusinessResults <User> IsUser = new BusinessResults <User>() { result = _userDal.GetLazyFirstOrDefault(x => x.UserName == data.UserName && x.Password == depassword, x => x.Role, x => x.Pages, x => x.Messages, x => x.PageDetails) }; if (IsUser.result != null) { if (IsUser.result.IsActive == false) { IsUser.AddError(ErrorMessageCode.UserIsNotActive, "User is not activated"); IsUser.AddError(ErrorMessageCode.CheckYourEmail, "Check your Email"); } } else { IsUser.AddError(ErrorMessageCode.UsernameOrPassWrong, "Password or Username is wrong"); } return(IsUser); }
public ActionResult Login(LoginViewModel model) { if (ModelState.IsValid) { BusinessResults <User> user = _userService.Login(model); if (user.Errors.Count > 0) { user.Errors.ForEach(x => ModelState.AddModelError(" ", x.Message)); return(View(model)); } FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe); user.result.LastLoginDate = DateTime.Now; System.Web.HttpContext.Current.Session["EncryptedUserID"] = generalhelper.Encrypt(user.result.UserID.ToString()); System.Web.HttpContext.Current.Session["UserID"] = user.result.UserID; System.Web.HttpContext.Current.Session["RoleID"] = user.result.RoleID; System.Web.HttpContext.Current.Session["RoleName"] = user.result.Role.RoleName; System.Web.HttpContext.Current.Session["UserName"] = user.result.UserName.ToString(); System.Web.HttpContext.Current.Session["PhotoPath"] = user.result.PhotoPath; System.Web.HttpContext.Current.Session["InsertDate"] = Convert.ToDateTime(user.result.CreatedDate).ToShortDateString(); _userService.Update(user.result); HttpCookie cookie = new HttpCookie("access"); if (model.RememberMe) { cookie.Values.Add("UserName", model.UserName); cookie.Values.Add("Password", model.Password); cookie.Values.Add("RoleID", user.result.RoleID.ToString()); cookie.Values.Add("RememberMe", model.RememberMe.ToString()); cookie.Expires = DateTime.Now.AddDays(15); Response.Cookies.Add(cookie); } else { cookie.Values.Add("UserName", ""); cookie.Values.Add("Password", ""); cookie.Values.Add("RoleID", ""); cookie.Values.Add("RememberMe", ""); cookie.Expires = DateTime.Now.AddDays(-1); Response.Cookies.Add(cookie); } Session["login"] = user.result; return(RedirectToAction("Index", "Dashboard")); } return(View(model)); }