Example #1
0
 public override void OnActionExecuting(ActionExecutingContext filterContext)
 {
     if (HttpContext.Current.User.Identity.IsAuthenticated && HttpContext.Current.Session["UserID"] == null)
     {
         FEEDbContext db    = new FEEDbContext();
         bool         Exist = db.Users.Any(e => e.Username == HttpContext.Current.User.Identity.Name);
         if (Exist)
         {
             var user        = db.Users.Where(e => e.Username == HttpContext.Current.User.Identity.Name).First();
             var userSession = new UserSession();
             userSession.Id           = user.Id;
             userSession.Name         = user.Name;
             userSession.RoleId       = user.RoleId;
             userSession.Username     = user.Username;
             userSession.DepartmentId = user.DepartmentId;
             HttpContext.Current.Session.Add("USER", userSession);
             HttpContext.Current.Session.Add("PERMISSION", AuthPermission.GetProfileService(user.Id));
         }
     }
     base.OnActionExecuting(filterContext);
 }
Example #2
0
        public ActionResult Login(LoginViewModel model, string ReturnUrl)
        {
            if (ModelState.IsValid)
            {
                var exist = db.Users.Any(x => x.Username == model.Username);

                if (exist)
                {
                    var user = db.Users.Where(e => e.Username.Equals(model.Username)).First();
                    if (user != null)
                    {
                        if (user.Password == XString.ToMD5(model.Password) && user.Status == (int)UserStatus.Activated)
                        {
                            setCookie(user.Username, model.RememberMe, user.RoleId);
                            var userSession = new UserSession();
                            userSession.Id           = user.Id;
                            userSession.Name         = user.Name;
                            userSession.RoleId       = user.RoleId;
                            userSession.Username     = user.Username;
                            userSession.DepartmentId = user.DepartmentId;

                            Session.Add("USER", userSession);
                            Session.Add("PERMISSION", AuthPermission.GetProfileService(user.Id));

                            if (ReturnUrl != null)
                            {
                                return(Redirect(ReturnUrl));
                            }
                            return(RedirectToAction("Index", "Home"));
                        }
                        ModelState.AddModelError("", "Sai tài khoản hoặc mật khẩu!");
                        return(View());
                    }
                }
            }
            return(View());
        }