代码之家  ›  专栏  ›  技术社区  ›  CVertex

正在检测ASP.NET MVC上的会话到期

  •  23
  • CVertex  · 技术社区  · 15 年前

    我已经构建了一个购物车,它使用会话状态来保存用户浏览商店时的购物车数据。

    如果在购物车的步骤1上长时间打开浏览器窗口,然后按“转到步骤2”,我的操作将引发错误,因为步骤2操作假定会话未过期并且购物车对象处于正确状态。

    我希望这个场景对我的用户更好,但是我想我需要以某种方式检测会话是否已过期,以便在下一个请求时我可以将它们抛出到步骤1。

    我发现下面的代码声称可以解决这个问题,但它对我不起作用。

    IsNewSession条件为true,但条件

    if ((null != sessionCookie) && (sessionCookie.IndexOf("ASP.NET_SessionId") >= 0)) {
       // handle expired session
    }
    

    总是返回false,它从不处理无效的会话。我搞糊涂了。

    这在ASP.NET(和MVC)中是可能的吗?

    2 回复  |  直到 9 年前
        1
  •  16
  •   Liam Joshua    10 年前

    1路

    将此代码放入 Init / Load 第2页的事件…

            if (Context.Session != null)
            {
                if (Context.Session.IsNewSession)
                {
                    string sCookieHeader = Request.Headers["Cookie"];
                    if ((null != sCookieHeader) && (sCookieHeader.IndexOf("ASP.NET_SessionId") >= 0))
                    {
    
                        if (Request.IsAuthenticated)
                        {
                            FormsAuthentication.SignOut();
                        }
                        Response.Redirect("Error Page");
                    }
                }
            }
    

    2路

    或者你可以检查 Session 在第2页继续处理对象之前,对象存在,如下所示:

    if (Session["Key"] != null)
    {
       Object O1 = (Object) Session["Key"]; 
    }
    else
    {
        Response.Redirect("ErrorPage.aspx");
    }
    
        2
  •  14
  •   Liam Joshua    10 年前

    国王的回答对我不起作用。我补充说 FormsAuthentication.SignOut() 在里面 OnActionExcuting() . 这个 Response.Redirect 行不通!

    if (Request.IsAuthenticated)
    {
        FormsAuthentication.SignOut();
    }
    

    这是我的全部方法

    public class SessionExpireFilterAttribute : ActionFilterAttribute
        {
    
            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))
                        {
                            string redirectOnSuccess = filterContext.HttpContext.Request.Url.PathAndQuery;
                            string redirectUrl = string.Format("?ReturnUrl={0}", redirectOnSuccess);
                            string loginUrl = FormsAuthentication.LoginUrl + redirectUrl;
                            if (ctx.Request.IsAuthenticated)
                            {
                                FormsAuthentication.SignOut();
                            }
                            RedirectResult rr = new RedirectResult(loginUrl);
                            filterContext.Result = rr;
                            //ctx.Response.Redirect("~/Home/Logon");
    
                        }
                    }
                }
    
                base.OnActionExecuting(filterContext);
            }
        }