【问题标题】:Detecting Session expiry on ASP.NET MVC检测 ASP.NET MVC 上的会话过期
【发布时间】:2010-12-02 05:31:31
【问题描述】:

我已经构建了一个购物车,它使用 Session State 在用户浏览商店时保存购物车数据。

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

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

我找到了以下声称可以解决问题的代码,但它对我不起作用。

IsNewSession 条件为真,但条件

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

总是返回 false 并且它从不处理无效会话。我很困惑。

这在 ASP.NET(和 MVC)中可行吗?

【问题讨论】:

    标签: c# asp.net asp.net-mvc session session-state


    【解决方案1】:

    方式 1

    将此代码放入Page 2的Init/Load事件中...

            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

    或者,您可以在第 2 页继续使用它之前检查 Session 对象是否存在,如下所示:

    if (Session["Key"] != null)
    {
       Object O1 = (Object) Session["Key"]; 
    }
    else
    {
        Response.Redirect("ErrorPage.aspx");
    }
    

    【讨论】:

    • 如果用户登录并关闭浏览器(会话仍然存在)...然后在会话过期后返回?
    【解决方案2】:

    国王的回答对我不起作用。我在OnActionExcuting() 中添加了FormsAuthentication.SignOut()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);
            }
        }
    

    【讨论】:

    • 嗨汤姆,忽略我的知识,但是我应该在哪里声明这个类(控制器???),我怎样才能使用这个属性???
    • Sanjay,在任何地方声明它。该类正在创建一个属性。通过应用 [SessionExpireFilterAttribute] 将属性与控制器类或操作方法相关联
    【解决方案3】:

    您需要在项目的Global.asax.cs文件中创建Session_OnEnd方法。

    这是我的代码,我能够在 ASP.NET MVC 上检测会话到期

    protected void Session_OnEnd(object sender, EventArgs e)
    {
        int userid = 0;
        userid = Convert.ToInt32(Session["UserID"]);
        if (userid != 0)
        {
            var userActivity = DependencyResolver.Current.GetService<IUserRepo>();
            var responce = userActivity.LogOutUsers(userid);
            if (responce == true)
            {
                Session.Clear();
                Session.Abandon();
            }
        }
    }
    

    more

    【讨论】:

      猜你喜欢
      • 2015-06-13
      • 2015-01-14
      • 2011-07-02
      • 1970-01-01
      • 2014-10-15
      • 2018-02-22
      • 2010-09-05
      • 2011-06-19
      相关资源
      最近更新 更多