【问题标题】:remove session after 10 hours of activity in my asp.net web site在我的 asp.net 网站中活动 10 小时后删除会话
【发布时间】:2018-09-18 23:11:00
【问题描述】:

我需要在用户活动 10 小时后删除会话。

我的问题是关于计算 10 小时。

我尝试将 ajax Timer (asp.net) 与 updatepanel 一起使用。我把它放在母版页中,但在页面之间切换会重置计时器。所以我尝试了这个想法: Asp.net Timer resets on page refresh 但它会导致页面每秒加载一次。

计算用户在网站上的活跃时间的最佳方法是什么?

这是使用此计时器的好方法还是有更好的方法?

或者,尽管有用户活动,是否有更好的方法来重置会话(如会话超时)?

【问题讨论】:

    标签: asp.net .net ajax session timer


    【解决方案1】:

    在 Web.Config 中设置会话超时 ASP.Net 将自行处理

    <configuration>
      <system.web>
        <sessionState 
          mode="InProc"
          cookieless="true"
          timeout="600" />
      </system.web>
    </configuration>
    

    您可以在此处参考更多信息: http://www.primaryobjects.com/2008/01/29/preventing-session-timeouts-in-c-asp-net-web-applications/

    要查找活跃用户,您可以使用服务器上的性能计数器:

    https://msdn.microsoft.com/en-us/library/fxk122b4.aspx

    【讨论】:

      【解决方案2】:

      解决办法是在Global.asax中添加

      void Session_Start(object sender, EventArgs e)
      {
          Session["SessionExpiry"] = DateTime.Now.AddHours(10);
      }
      
      void Application_AcquireRequestState(object sender, EventArgs e)
      {
        try
        {
            DateTime expiry = (DateTime)base.Context.Session["SessionExpiry"];
      
            if (expiry <= DateTime.Now)
            {
                Session.Abandon();
      
                // the abandoned session won't kick in until the next page request.
                // here I am redirecting back to the current page, but you may want to do something else
                // like redirect to a "Session Expired" page.
                Response.Redirect(Request.RawUrl);
                base.Context.ApplicationInstance.CompleteRequest();
            }
        }
        catch
        {
        }
      }
      

      来源: https://www.codeproject.com/Questions/1175605/How-to-timeout-session-in-minutes-in-ASP-NET

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-03-01
        • 2011-10-28
        • 2010-12-22
        • 2013-01-17
        • 2017-12-10
        • 1970-01-01
        • 2012-08-12
        相关资源
        最近更新 更多