简体   繁体   English

ASP.NET会话状态超时问题

[英]ASP.NET Session State Timeout Problem

I am trying to detect a session state timeout in my asp.net application and am unable to do so. 我试图在我的asp.net应用程序中检测会话状态超时,但无法做到这一点。 I have a base class that derives from System.Web.UI.Page as follows:- 我有一个派生自System.Web.UI.Page的基类,如下所示:

public class BasePageSessionExpire : Page
{
    override protected void OnInit(EventArgs e)
    {
        base.OnInit(e); 
        if (Context.Session != null)
        {                
            if (Session.IsNewSession)
            {                    
                string szCookieHeader = Request.Headers["Cookie"];
                if ((null != szCookieHeader) && (szCookieHeader.IndexOf("ASP.NET_SessionId") > 0))
                {
                    Session.Abandon();
                    Response.Redirect("~/SessionExpired.aspx",true);
                }
            }
        }
    }
}

All the pages I need session state checking on derive from this base class instead of "System.Web.UI.Page". 我需要进行会话状态检查的所有页面均源自此基类,而不是“ System.Web.UI.Page”。 Also, all these pages have EnableSessionState="True". 此外,所有这些页面都具有EnableSessionState =“ True”。 I have a blank Session_Start() method in my global.asax file if that is relevant at all. 我的global.asax文件中有一个空的Session_Start()方法(如果有的话)。

For some reason after the first request, the "Session.IsNewSession" property is always false. 由于第一个请求之后的某种原因,“ Session.IsNewSession”属性始终为false。 It is true only for the first request and then is always false. 仅对第一个请求为true,然后始终为false。 I have my timeout set to 1 minute. 我将超时设置为1分钟。 The session never seems to timeout. 该会话似乎从未超时。 What am I missing here ? 我在这里想念什么?

Also I have implemented a state server in SQL Server 2008. This is not an in-proc session state implementation. 另外,我已经在SQL Server 2008中实现了状态服务器。这不是进程内会话状态实现。

Thanks in advance. 提前致谢。

Since you have Session_start event in global.asax file, after the session expires, Session_start fired again. 由于您在global.asax文件中有Session_start事件,因此会话过期后,Session_start会再次触发。 that means session Id is already created and so oninit event IsNewSessionId would return false because session Id is already created. 这意味着会话ID已创建,因此oninit事件IsNewSessionId将返回false,因为会话ID已创建。

So removing the Session_Start event will work. 因此,删除Session_Start事件将起作用。

Let me know if this solution works for you. 让我知道此解决方案是否适合您。

An ASP.Net session is started on the user's first request to a server. 在用户第一次向服务器请求时启动ASP.Net会话。 This is the only time that IsNewSession will be true. 这是IsNewSession为true的唯一时间。

Calling Session.Abandon() removes the current session, and then if you redirect the user to the SessionExpired page, that will start a new session. 调用Session.Abandon()会删除当前会话,然后如果将用户重定向到SessionExpired页面,则会启动一个新会话。

I'm not sure why the timeout would not be firing. 我不确定为什么超时不会触发。 Have you put code in the Global.asax Session_End event to verify that sessions aren't ending? 您是否将代码放入Global.asax Session_End事件中以验证会话没有结束?

In your application , If no Session_Start event, Then each time IsNewSession would return true for each postback unless until you store some value in session. 在您的应用程序中,如果没有Session_Start事件,则每次IsNewSession对于每个回IsNewSession都将返回true,除非您在session中存储了一些值。

Once you store some value in session, the IsNewSession would return false next time even many post back. 一旦在会话中存储了一些值, IsNewSession将返回false,即使有很多回发。

Once session expires, IsNewSession would return true till no session stores. 一旦会话过期, IsNewSession将返回true,直到没有会话存储。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM