开发者

what happens with session_start in global.asax if session timeouts?

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 us开发者_高级运维er 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.


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:

<configuration>
  <sessionstate 
      mode="inproc"
      cookieless="false" 
      timeout="20" />
</configuration>

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.


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(object sender, EventArgs e) { // Code that runs when a new session is started

    string RootURL = Request.ApplicationPath;
    if (!RootURL.EndsWith("/"))
        RootURL += "/";
    Globals._rootURL = RootURL;
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜