大量具有相同会话 ID 的 Session_Start

发布于 2024-08-20 17:46:45 字数 740 浏览 4 评论 0原文

我正在我的开发盒上运行 ASP.NET 网站(Vista/IIS7 上的 .NET 2.0)。 global.asax.cs 中的 Session_Start 方法记录对文件 (log4net) 的每次调用。 Session_End 方法还会记录每次调用。

我使用 InProc 会话状态,并将会话超时设置为 5 分钟(以避免等待 20 分钟)。

我访问了该网站,等待 5 分钟,我看到了 Session_End 日志记录。然后我F5该网站。浏览器仍然保留会话 cookie 并将其发送到服务器。调用 Session_Start 并使用相同的会话 id 创建一个新会话(顺便说一句:我需要它是相同的会话 id,因为它用于在数据库中存储数据)。

结果: 每次我在之前结束的会话上按 F5 时,都会调用 Session_Start 方法,执行请求并立即调用 Session_End 方法。

当我打开不同的浏览器时,Session_Start 方法仅被调用一次。然后,5 分钟后,Session_End 每按一次 F5 都会导致执行 Session_Start/request/Session_End 序列。

web.config 相关部分:

<system.web>
  <compilation debug="true" />
  <sessionState timeout="2" regenerateExpiredSessionId="false" />
</system.web>

I'm running a ASP.NET website on my development box (.NET 2.0 on Vista/IIS7).
The Session_Start method in global.asax.cs logs every call to a file (log4net).
The Session_End method also logs every call.

I'm using InProc session state, and set the session timeout to 5 mins (to avoid waiting for 20 mins).

I hit the website, wait for 5 minutes unit I see the Session_End logging. Then I F5 the website. The browsers still has the session cookie and sends it to the server. Session_Start is called and a new session is created using the same session id (btw: I need this to be the same session id, because it is used to store data in database).

Result:
Every time I hit F5 on a previously ended session, the Session_Start method is called, the request is executed and the Session_End method is called immediately.

When I open a different browser, the Session_Start method is called just once. Then after 5 minutes the Session_End each F5 causes the Session_Start/request/Session_End sequence to execute.

web.config relevant section:

<system.web>
  <compilation debug="true" />
  <sessionState timeout="2" regenerateExpiredSessionId="false" />
</system.web>

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

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

发布评论

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

评论(1

維他命╮ 2024-08-27 17:46:45

regenerateExpiredSessionId 设置与 仅限无 cookie 的 URL,它不会影响将重用的会话 cookie 的行为。

您遇到的问题是由于 ASP.NET 2.0/3.5 根据会话是否正在使用来处理会话的方式造成的。在正常情况下,它不会尝试保留会话,直到第一次使用它,因此不会发出会话 cookie(如果它不存在)。第一次使用会话时,会在服务器上创建一个会话并发出一个会话 cookie。

现在,当先前的会话重新启动但未使用时,ASP.NET 会变得有点混乱。它尝试立即放弃未使用的(重新启动的)会话,因为它不是必需的,这会引发提前的 Session_End。但是,它不会删除预先存在的会话 cookie,因此每个后续请求都会重复该序列,重新启动然后终止会话,直到删除 cookie 或使用会话。

在 .Net 4.0 中,此行为已更改,在这种情况下不再触发该事件。

The regenerateExpiredSessionId setting relates to cookieless URLs only, it doesn't affect the behaviour of a session cookie which will be reused.

The issue you are experiencing is because of the way ASP.NET 2.0/3.5 handles sessions based on whether it's in use. In normal circumstances it does not try to persist a session until the first time it's used and therefore does not issue a session cookie (if it doesn't exist). The first time session is used, a session is created on the server and a session cookie issued.

Now when a previous session is restarted but not used, then ASP.NET gets a little confused. It tries to abandon the unused (restarted) session immediately as it's not required, which raises an early Session_End. However it does not delete the pre-existing session cookie, and hence every subsequent request repeats the sequence, restarting and then terminating the session until the cookie is deleted or the session is used.

In .Net 4.0 this behaviour has changed, and the event no longer fires in this case.

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