Ejemplo n.º 1
0
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            // Do not execute the filter logic for User/Login
            if (filterContext.RouteData.GetRequiredString("controller").Equals("User", StringComparison.CurrentCultureIgnoreCase) &&
                filterContext.RouteData.GetRequiredString("action").Equals("Login", StringComparison.CurrentCultureIgnoreCase))
            {
                if (!filterContext.HttpContext.Request.IsAjaxRequest())
                {
                    return;
                }
                else
                {
                    filterContext.Result = new JsonResult
                    {
                        JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                        Data = new { Valid = false, RedirectUrl = FormsAuthentication.LoginUrl }
                    };
                }
            }

            HttpSessionStateBase session = filterContext.HttpContext.Session;
            var username = HttpContext.Current.User != null ? HttpContext.Current.User.Identity.Name : string.Empty;

            _userFacade = new UserFacade();
            _userFacade.CheckExceededMaxConcurrent(username, session);

            if (!filterContext.HttpContext.Request.IsAjaxRequest())
            {
                if (((session["sessionid"] == null) && (!session.IsNewSession)) || (session.IsNewSession))
                {
                    // Clear cache
                    var cacheKey = string.Format(CultureInfo.InvariantCulture, "{0}_user_info", username);
                    if (HttpRuntime.Cache[cacheKey] != null)
                    {
                        HttpRuntime.Cache.Remove(cacheKey);
                    }

                    session.RemoveAll();
                    session.Clear();
                    session.Abandon();

                    string returnUrl = GetReturnUri(filterContext);

                    RouteValueDictionary dict = new RouteValueDictionary();
                    dict.Add("controller", "User");
                    dict.Add("action", "Login");

                    if (!string.IsNullOrWhiteSpace(returnUrl))
                    {
                        dict.Add("returnUrl", returnUrl);
                    }

                    filterContext.Result = new RedirectToRouteResult(dict);
                }

                base.OnActionExecuting(filterContext);
            }
            else
            {
                if (((session["sessionid"] == null) && (!session.IsNewSession)) || (session.IsNewSession))
                {
                    // Clear cache
                    var cacheKey = string.Format(CultureInfo.InvariantCulture, "{0}_user_info", username);
                    if (HttpRuntime.Cache[cacheKey] != null)
                    {
                        HttpRuntime.Cache.Remove(cacheKey);
                    }

                    session.RemoveAll();
                    session.Clear();
                    session.Abandon();

                    filterContext.Result = new JsonResult
                    {
                        JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                        Data = new { Valid = false, RedirectUrl = FormsAuthentication.LoginUrl }
                    };
                }
            }
        }