如果会话超时,global.asax 中的 session_start 会发生什么情况?
我有一个多域 Web 应用程序,它根据用户使用的 URL 对用户进行不同的处理。
我使用 Session["data"] 来保存有关用户的信息,并使用 Global.asax 中的 Session_Start["data"] 启动此会话。
一切正常,但我想知道不活动后会发生什么。一定时间后会话将超时。如果发生这种情况,Global.asax 是否将其视为新用户并将再次为此用户启动 Session_Start?
Session["data"] 会随着每次页面加载/重新加载而更新吗?或者因为它只启动一次并且会在某个确切的时间超时?
我试图尽可能清楚地表达这个问题。
谢谢。
I have multidomain web application which treats users differently based on URL they use.
I am using Session["data"] to keep information about user and starting this session with Session_Start["data"] in Global.asax.
All works fine but I would like to know what happens after inactivity. After certain time session will timeout. If that happens is Global.asax treating this as new user and will again start Session_Start for this user?
And will Session["data"] get updated with every page load/reload? Or because it starts just once and will timeout in some exact time?
I tried to make this question as clear as possible.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
每次服务器被该用户击中时,会话都会更新/保持活动状态。您在 Web 配置文件中设置超时,它是一个滑动值,因此每次有服务器请求时它都会重新启动。
像这样:
当会话超时时,下次有请求时,Session_Start 将执行。如果您从代码中的其他位置访问 Session[data],您应该检查以确保它不为 null,因为如果会话超时并且您尝试访问它,它将抛出 NullReferenceException。
Session will renew/keep-alive everytime the server gets hit by that user.You set the timeout in the web config file and it is a sliding value, so it restarts again everytime there is a server request.
something like this:
When the session times out, the next time there is a request, the Session_Start will execute. If you are accessing Session[data] from anywhere else in the code, you should check to make sure it is not null as it will throw a NullReferenceException if the session has timed out and you are trying to access it.
当用户首次访问站点上的 .NET URL(如 .aspx 页面,但不是 .html 或其他静态文件)时,新会话就会启动。该会话将持续到超时或应用程序被终止(重新启动/崩溃/回收)。默认的.NET超时是20分钟;因此,只要用户持续访问 .aspx 页面且不间断时间超过 20 分钟,会话就会持续。
在此期间,您可以将与该用户相关的信息存储在 Session 对象中。它本质上是一个哈希表,您可以使用您为其定义键的对象来填充该哈希表。就您而言,您正在使用 Session["data"],但您实际上可以使用任何您想要的密钥。
然而,会话以及存储在会话哈希表中的数据非常脆弱(请参阅上面它可能消失的所有方式)。您不应该依赖它来保存任何无法轻松重建的重要内容(例如,在 Session_Start 中)。所以它实际上有两个作用:维护状态(这样你就知道从一个页面到另一个页面它仍然是同一个用户);作为用户特定的缓存,您可以将数据保存在内存中以更快地完成操作。
根据定义,Session_Start 每个会话仅运行一次。如果您需要在多个会话中识别单个用户,您将需要使用更永久的东西,例如设置您自己的具有远期到期时间的 cookie。您可以在这样的 cookie 中放置一个 ID,让您知道这是用户 12345(事实上,Session_Start 只是查找“永久”cookie 并将有关该现有用户的数据与此新会话连接起来的地方)。
如果您想存储有关在多个会话中幸存的用户的数据,则必须将其存储在更永久的地方 - 数据库是最明显的解决方案。当它们返回时,您可以在会话哈希表中缓存一些数据——Session_Start 也正是执行此操作的地方。希望这有帮助。
A new session starts when a user first visits a .NET URL (like an .aspx page, but not a .html or other static file) on your site. That session lasts until it times out or the application is killed (restarted/crashes/recycled). The default .NET timeout is 20 minutes; so a session will last as long as the user keeps hitting .aspx pages with no breaks longer than 20 minutes.
During that time, you can store information in the Session object that relates to that user. It is essentially a hashtable that you can populate with objects for which you define keys. In your case, you are using Session["data"], but you could use any key you want, really.
However a session, and the data you store in the Session hashtable, is very fragile (see all the ways it can die above). You shouldn't rely on it to keep anything important that can't be reconstructed easily (in Session_Start, for example). So it really serves two roles: maintaining state (so you know it is still the same user from page to page); and as a user-specific cache where you can keep data in memory to do things more quickly.
Session_Start just runs once per session--by definition. If you need to identify a single user over multiple sessions, you will need to use something more permanent like setting your own cookie with a far-future expiration. You can put an ID in such a cookie that lets you know this is user 12345 (in fact, Session_Start is just the place to look for your "permanent" cookie and connect your data about that existing user with this new session).
And if you want to store data about a user that survives multiple sessions, you will have to store that somewhere more permanent--a database being the most obvious solution. When they come back, you can cache some of that data in the Session hashtable--and Session_Start is just the place to do that as well. Hope this helps.
protected void Session_Start(对象发送者,EventArgs e)
{
// 新会话启动时运行的代码
protected void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started