IIS 7 中的会话超时如何工作?

发布于 2025-01-07 19:13:42 字数 559 浏览 3 评论 0原文

在 web.config 中,我将 sessionState 中的超时设置为 20 分钟。根据 MSDN,此超时指定会话在被放弃之前可以空闲的分钟数。在 IIS 7 中,DefaultWebSite->Session State->Cookie Settings->Time Out 会自动填充 web.config 中设置的超时值,在我的例子中为 20 分钟。另外,应用程序池->DefaultAppPool->高级设置->idleTimeout,我设置为10分钟。

然后我做了两个测试: 第一次测试:我在下午 3:45 登录我的网络应用程序,空闲了 10 分钟。下午 3:55,我尝试使用我的应用程序,但被踢出了。我认为idleTimeout发挥了作用。

第二次测试:我在下午 4:00 登录我的网络应用程序,并在下午 4:05、4:10、4:15 和 4:20 玩该应用程序。我预计下午 4:20 就会被赶出去。但我不是。我认为 IIS 7 中的会话状态超时(20 分钟)是 Web 代理要求用户重新进行身份验证之前用户会话可以处于活动状态的最长时间。从这次测试来看,显然不是。谁能向我解释一下吗?另外,我如何设置上述情况的超时?

In web.config, I set timeout in the sessionState to 20 minutes. According to MSDN, this timeout specifies the number of minutes a session can be idle before it is abandoned. In IIS 7, DefaultWebSite->Session State->Cookie Settings->Time Out automatically is populated with timeout value set in web.config, which in my case is 20 minutes. Also, Application Pools->DefaultAppPool->Advanced Settings->idleTimeout, I set it to 10 minutes.

Then I made two tests:
First test: I logged in my web app at 3:45pm, idling for 10 minutes. At 3:55pm, I tried to use my app, I got kicked out. I think the idleTimeout comes in play.

Second test: I logged in my web app at 4:00pm, play with the app at 4:05pm, 4:10pm, 4:15pm and 4:20pm. I expected being kicked out at 4:20pm. But I was not. I thought the session state timeout (20min) in IIS 7 is the the maximum amount of time a user session can be active before the Web Agent challenges the user to re-authenticate. Apparently from this test, it is not. Can anyone explain that to me? Also, how could I set the timeout for above case?

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

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

发布评论

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

评论(1

多孤肩上扛 2025-01-14 19:13:42

会话超时是一种滑动超时,用户每次访问服务器时都会将其重置为配置的值。

如果在一段时间内没有向您的应用程序发出任何请求,则应用程序空闲超时将启动。

因此,通常的场景是:

用户 A用户 B会话状态
12:00访问 Page1A:新会话,超时:20 分钟
12:02访问 Page2A:超时重置:20 分钟
12:10访问第 1 页A:超时:12 分钟; B:新:20 分钟
12:15访问第2页A:超时:07分钟; B:超时:20 分钟
12:22A:超时; B:还剩 13 分钟
12:32应用程序关闭(达到空闲时间)
12:35访问第 3 页A:新会话开始

如果用户 A 在 12:22 之后返回站点,他们将拥有一个全新的会话,并且您可以设置任何值以前存储在那里的将会丢失。

确保会话在应用程序重新启动后持续存在的唯一方法是配置 SessionState 服务或 SQL 会话状态,并确保您已 配置 machine.key,以便每次服务器重新启动时不会自动生成。

如果您使用标准 ASP.NET 机制进行身份验证,则 ASP.NET 将向每个用户发出两个 cookie:

  1. 身份验证令牌:由 身份验证超时设置,允许用户在 cookie 尚未过期的情况下自动登录到您的网站,这是可以修复的或滑动,默认为 30 分钟,这意味着他们的身份验证令牌可以处理比会话更长的“空闲”时间。
  2. 会话令牌:由会话超时设置控制,允许您的应用程序在访问生命周期内存储和访问每个用户的值。

这两个 cookie 均使用 MachineKey 进行加密 - 因此,如果您的应用程序回收并生成新密钥,则这些令牌都无法解密,从而需要用户登录并创建新会话。


回复评论:

  1. 20 分钟会话超时与您放置在用户会话对象中的项目相关 (HttpSessionState) 使用 Session.Add(string, object) 方法。
  2. 这要看情况。如果您已正确配置 machine.key身份验证令牌仍然有效,并且如果您的会话不再是“InProc”,这些令牌也将在应用程序重新启动后持续存在,并且仍然可读 - 请参阅上面的注释。

Session time-out is a sliding time-out that is reset for a user to the configured value each time they visit the server.

The Application Idle time-out kicks in if there have been no requests to your application for that period of time.

The usual scenarios is therefore:

TimeUser AUser BSession States
12:00Visits Page1A: New Session, Time-out: 20 minutes
12:02Visits Page2A: Time-out reset: 20 minutes
12:10Visits Page1A: Time-out: 12 min; B: New: 20 minutes
12:15Visits Page2A: Time-out: 07 min; B: Time-out: 20 min
12:22A: times out; B: 13 min remaining
12:32Application Shuts Down (Idle time reached)
12:35Visits Page3A: New Session Starts

If User A were to return to the site after 12:22 they would have a completely new session, and any values you've stored in there previously would be lost.

The only way to ensure that a session persists over application restarts is to configure either a SessionState service or SQL Session States, and ensure that you've configured the machine.key so that's it not AutoGenerated each time the server restarts.

If you're using the standard ASP.NET mechanisms for authentication, then ASP.NET will will issue two cookies to each user:

  1. Authentication Token: Controlled by the Authentication time-out setting, allows the user to be auto logged in to your site if the cookie hasn't expired, this can be fixed or sliding, and defaults to 30 minutes, which means their authentication token can cope with a longer "idle" period than their session.
  2. Session Token: Controlled by the Session Time-out setting, allows your application to store and access per-user values during the lifetime of their visit.

Both of those cookies are encrypted using the MachineKey - so if your application recycles and generates a new key neither of those tokens can be decrypted, requiring the user to log in and create a new session.


Responding to comments:

  1. The 20 minute session time-out relates to items you've placed in the users session object (HttpSessionState) using the Session.Add(string, object) method.
  2. That depends. If you've correctly configured the machine.key, authentication tokens will still be valid, and if your sessions are no longer "InProc" these will also persist through application restarts and will still be readable - see notes above.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文