ASP.NET 会话状态超时问题

发布于 2024-08-28 22:56:03 字数 1045 浏览 1 评论 0原文

我试图在我的 ASP.NET 应用程序中检测会话状态超时,但无法执行此操作。我有一个从 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);
                }
            }
        }
    }
}

我需要会话状态检查的所有页面都从该基类派生而不是“System.Web.UI.Page”。此外,所有这些页面都有 EnableSessionState="True"。如果相关的话,我的 global.asax 文件中有一个空白的 Session_Start() 方法。

由于某种原因,在第一次请求后,“Session.IsNewSession”属性始终为 false。仅对第一个请求为 true,之后始终为 false。我将超时设置为 1 分钟。会话似乎永远不会超时。我在这里缺少什么?

我还在 SQL Server 2008 中实现了状态服务器。这不是进程内会话状态实现。

提前致谢。

I am trying to detect 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

当梦初醒 2024-09-04 22:56:03

由于 global.asax 文件中有 Session_start 事件,因此在会话过期后,Session_start 会再次触发。这意味着会话 Id 已经创建,因此 oninit 事件 IsNewSessionId 将返回 false,因为会话 Id 已经创建。

因此删除 Session_Start 事件就可以了。

让我知道这个解决方案是否适合您。

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.

葬花如无物 2024-09-04 22:56:03

ASP.Net 会话在用户向服务器发出第一个请求时启动。这是 IsNewSession 唯一一次为 true。

调用 Session.Abandon() 会删除当前会话,然后如果您将用户重定向到 SessionExpired 页面,则会启动一个新会话。

我不确定为什么超时不会被触发。您是否已将代码放入 Global.asax Session_End 事件中以验证会话没有结束?

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?

垂暮老矣 2024-09-04 22:56:03

在您的应用程序中,如果没有 Session_Start 事件,那么每次 IsNewSession 都会为每次回发返回 true ,除非您在会话中存储了一些值。

一旦您在会话中存储了一些值,IsNewSession 下次即使有很多回发也会返回 false。

一旦会话过期,IsNewSession将返回true,直到没有会话存储。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文