【问题标题】:Always Getting redirected to SessionTimeOut page ASP.net MVC总是被重定向到 SessionTimeOut 页面 ASP.net MVC
【发布时间】:2011-10-21 08:06:13
【问题描述】:

我在每个操作上使用 SessionExpireFilter 来检查会话是否已过期。如果会话已过期,则它将用户重定向到 sessionTimeoutPage 即到 membersController 和 SessionTimeOut 视图

过滤器看起来像-

 public class SessionExpireFilterAttribute : ActionFilterAttribute
{
    /// <summary>
    /// Called when [action executing].
    /// </summary>
    /// <param name="filterContext">The filter context.</param>
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        HttpContext ctx = HttpContext.Current;

        // check if session is supported
        if (ctx.Session != null)
        {
            // check if a new session id was generated
            if (ctx.Session.IsNewSession)
            {
                // If it says it is a new session, but an existing cookie exists, then it must
                // have timed out
                string sessionCookie = ctx.Request.Headers["Cookie"];
                if ((null != sessionCookie) && (sessionCookie.IndexOf("ASP.NET_SessionId") >= 0))
                {
                    if (ctx.Request.IsAuthenticated)
                    {
                        FormsAuthentication.SignOut();
                    }

                    //HttpCookie mycookie = new HttpCookie("ASP.NET_SessionId");
                    //mycookie.Expires = DateTime.MinValue;
                    //ctx.Response.Cookies.Add(mycookie);
                    //ctx.Session.Clear();

                    RouteValueDictionary redirectTargetDictionary = new RouteValueDictionary();
                    redirectTargetDictionary.Add("action", "SessionTimeOut");
                    redirectTargetDictionary.Add("controller", "Membership");
                    filterContext.Result = new RedirectToRouteResult(redirectTargetDictionary);

                }
            }
        }

        base.OnActionExecuting(filterContext);
    }
}

问题是我有一个会员控制器登录操作方法,它也有那个过滤器。它检查会话是否过期并总是找到 ASP.NET_SessionId cookie 并一次又一次地重定向到 sessionTimeout 页面(其中有一个登录页面的链接)

如果有人能提供帮助,那就太好了。

【问题讨论】:

    标签: asp.net-mvc-3


    【解决方案1】:

    要使此解决方案起作用,您需要连接 Global.asax 中的 Session_Start 事件。如果您不这样做,则 Session.IsNewSession 将始终为真(这就是 ASP.NET 运行时的工作方式)。有时还需要在 Session 中存储一些东西(任何东西)。

    【讨论】:

    • 我认为这很好,因为我再次登录并且它是一个 nw 会话。我遇到的问题是 ASP.Net_session cookie 即使在新会话中也始终存在
    • 相信我,这就是运行时的工作方式(执行登录与 Session 无关),只需连接事件并检查即可。
    • 那次活动期间您是否尝试将某些内容放入 Session 中?刚刚在我的环境中测试过,似乎工作正常。
    猜你喜欢
    • 2023-04-09
    • 2013-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 2017-11-27
    • 2018-12-13
    • 1970-01-01
    相关资源
    最近更新 更多