views:

3468

answers:

10

By default the session expiry seems to be 20 minutes.

Update: I do not want the session to expire until the browser is closed.

Update2: This is my scenario. User logs into site. Plays around the site. Leaves computer to go for a shower (>20 mins ;)). Comes back to computer and should be able to play around. He closes browser, which deletes session cookie. The next time he comes to the site from a new browser instance, he would need to login again.

In PHP I can set session.cookie_lifetime in php.ini to zero to achieve this.

+1  A: 

This is default. When you have a session, it stores the session in a "Session Cookie", which is automatically deleted when the browser is closed.

If you want to have the session between 2 browser session, you have to set the Cookie.Expired to a date in the feature.

Because the session you talk about is stored by the server, and not the client you can't do what you want.

But consider not using ASP.NET server side session, and instead only rely on cookies.

Thomas Jespersen
A: 

There's no way to explicitly clear the session if you don't communicate in some way between the client and the server at the point of window closing, so I would expect sending a special URI request to clear the session at the point of receiving a window close message.

My Javascript is not good enough to give you the actual instructions to do that; sorry :(

Petesh
A: 

You cant, as you can't control how the html client response.

Actually why you need to do so? As long as no one can pick up the session to use again, it would expire after that 20 minutes. If resources does matter, set a more aggressive session expiry (most hosting companies did that, which is horribly annoying) or use less objects in session. Try to avoid any kind of object, instead just store the keys for retrieving them, that is a very important design as it helps you to scale your session to a state server when you get big.

goodwill
+1  A: 

Unfortunately due to the explicit nature of the web and the fact there is no permanent link between a website server and a users browser it is impossible to tell when a user has closed their browser. There are events and JavaScript which you can implement (e.g. onunload) which you can use to place calls back to the server which in turn could 'kill' a session - Session.Abandon();

You can set the timeout length of a session within the web.config, remember this timeout is based on the time since the last call to the server was placed by the users browser.

Browser timedout did not added.

Toby Mills
+2  A: 

You could set a short session timeout (eg 5 mins) and then get the page to poll the server periodically, either by using Javascript to fire an XmlHttpRequest every 2 minutes, or by having a hidden iframe which points to a page which refreshes itself every 2 minutes.

Once the browser closes, the session would timeout pretty quickly afterwards as there would be nothing to keep it alive.

Luke Bennett
+2  A: 

This is not a new problem, there are several scenarios that must be handled if you want to catch all the ways a session can end, here are general examples of some of them:

  1. The browser instance or tab is closed.
  2. User navigates away from your website using the same browser instance or tab.
  3. The users loses their connection to the internet (this could include power loss to user's computer or any other means).
  4. User walks away from the computer (or in some other way stops interacting with your site).
  5. The server loses power/reboots.

The first two items must be handled by the client sending information to the server, generally you would use javascript to navigate to a logout page that quickly expires the session.

The third and fourth items are normally handled by setting the session state timeout (it can be any amount of time). The amount of time you use is based on finding a value that allows the users to use your site without overwhelming the server. A very rough rule of thumb could be 30 minutes plus or minus 10 minutes. However the appropriate value would probably have to be the subject of another post.

The fifth item is handled based on how you are storing your sessions. Sessions stored in-state will not survive a reboot since they are in the computer's ram. Sessions stored in a db or cookie would survive the reboot. You could handle this as you see fit.

In my limited experience when this issue has come up before, it's been determined that just setting the session timeout to an acceptable value is all that's needed. However it can be done.

Dave_H
A: 

Correct me if I am misreading the intent of your question, but the underlying question seems to be less about how to force the session to end when a user closes the browser and more about how to prevent a session from ending until the browser is closed.

I think the real answer to this is to re-evaluate what you are using sessions to do. If you are using them to maintain state, I agree with the other responses that you may be out of luck.

However, a preferred approach is to use a persistent state mechanism with the same scope as the browser session such as a cookie that expires when the browser is closed. That cookie could contain just enough information to re-initiate the session on the server if it has expired since the last request. Combined with a relatively short (5-10 min) session timeout, I think this gives you the best balance between server resource usage and not making the user continually "re-boot" the site.

JohnFx
+3  A: 

If you want to extend the session beyond 20 minutes, you change the default using the IIS admin or you can set it in the web.config file. For example, to set the timeout to 60 minutes in web.config:

<configuration>
  <system.web>
    <sessionState timeout="60" />
    ... other elements omitted ...
  </system.web>
  ... other elements omitted ....
</configuration>

You can do the same for a particular user in code with:

Session.Timeout = 60

Whichever method you choose, you can change the timeout to whatever value you think is reasonable to allow your users to do other things and still maintain their session.

There are downsides of course: for the user, there is the possible security issue of leaving their browser unattended and having it still logged in when someone else starts to use it. For you there is the issue of memory usage on the server - the longer sessions last, the more memory you'll be using at any one time. Whether or not that matters depends on the load on your server.

If you don't want to guesstimate a reasonable extended timeout, you'll need to use one of the other techniques already suggested, requiring some JavaScript running in the browser to ping the server periodically and/or abandon the session when a page is unloaded (provided the user isn't going to another page on your site, of course).

Simon Forrest
A: 

Oh you have rewritten the question.

That one is absolutely feasible, as long as javascript is alive. Use any timed ajax will do. Check with prototype library http://www.prototypejs.org PeriodicalExecutor or jQuery with the ajax + timer plugin. Setup a dummy page which your executor will call from time to time, so your session is always alive unless if he logouts (kill the ajax timer in the same time) or close browser (which means the executor is killed anyway)

goodwill
A: 

hai, when i click the log out button it will be loged out...after if i click the back in ie6 then it will go to the application....how can i solve the problam?any one please help me... thanks ilavarasu