ASP.NET MVC 4会话到期

时间:2014-07-22 13:27:19

标签: asp.net .net asp.net-mvc asp.net-mvc-4

我通过以下方式检测到会话到期:

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
    if (null != filterContext.HttpContext.Session)
    {
        // Check if we have a new session
        if (filterContext.HttpContext.Session.IsNewSession)
        {
            string cookie = filterContext.HttpContext.Request.Headers["Cookie"];
            // Check if session has timed out
            if ((null != cookie) && (cookie.IndexOf("ASP.NET_SessionId") >= 0))
            {
                RouteValueDictionary redirectTargetDictionary = new RouteValueDictionary();

                redirectTargetDictionary.Add("action", "SessionExpired");
                redirectTargetDictionary.Add("controller", "Error");
                redirectTargetDictionary.Add("timeout", "true");

                filterContext.Result = new RedirectToRouteResult(redirectTargetDictionary);
                return;
            }
        }
        else { //continue with action as usual
            base.OnActionExecuting(filterContext);
        }
    }
}

问题在于,每次运行应用程序时,都会触发SessionExpired操作(当我第一次请求应用程序URL时,它会在cookie中找到ASP.NET_SessionId字符串),然后是我加载另一个URL应用程序工作正常。如何解决?

0 个答案:

没有答案