开发者

ASP.NET Session State Timeout Problem

开发者 https://www.devze.com 2022-12-26 13:41 出处:网络
I am trying to detect开发者_如何学Go a session state timeout in my asp.net application and am unable to do so. I have a base class that derives from System.Web.UI.Page as follows:-

I am trying to detect开发者_如何学Go a session state timeout in my asp.net application and am unable to do so. I have a base class that derives from System.Web.UI.Page as follows:-

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". Also, all these pages have EnableSessionState="True". I have a blank Session_Start() method in my global.asax file if that is relevant at all.

For some reason after the first request, the "Session.IsNewSession" property is always false. It is true only for the first request and then is always false. I have my timeout set to 1 minute. 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.

Thanks in advance.


Since you have Session_start event in global.asax file, after the session expires, Session_start fired again. that means session Id is already created and so oninit event IsNewSessionId would return false because session Id is already created.

So removing the Session_Start event will work.

Let me know if this solution works for you.


An ASP.Net session is started on the user's first request to a server. This is the only time that IsNewSession will be 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.

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?


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.

Once you store some value in session, the IsNewSession would return false next time even many post back.

Once session expires, IsNewSession would return true till no session stores.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号