views:

65

answers:

3

I am creating a content management system but there is one problem. What I want to do in my website is that when a user opens the website a new sessionid is created for that user, and when the user closes the website, the sessionid is cleared. How can I can do it?

+2  A: 

There isn't a way to detect when a user closes your site or navigates away from your site. After a given period of inactivity (default 20 min), the user's session object will expire and be automatically cleared. I suggest checking out the MSDN article on Session, which will go over your options for tweaking your application's Session.

If on the other hand you're looking to perform custom logic when a user's session begins or expires (due to inactivity), you can add a Global.asax file to your application and use the Session_Start and Session_End methods for that.

wsanville
A: 

An easy solution which works independently of Asp.net is to put timestamps on your session data and delete expired entries periodically.

ko-dos
+2  A: 

That is basically the default behaviour of sessions in ASP.NET.

When a user closes the browser, the non-persistent cookie maintaining the Session ID will be dropped. This wouldn't actually expire the session, but since no reference to it will exist anymore, it will expire automatically within the default 20 minutes. Users revisiting your site after closing the browser window would have to use a new session. (Source)

Daniel Vassallo
thank's friends
mayur
@mayur: If you found one of the answers helpful, you may want to mark it as accepted by clicking on the green tick next to the answer. This will help you get more answers in the future should you ask further questions on Stack Overflow.
Daniel Vassallo