Click here to Skip to main content
15,304,276 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Sir/Mam,

I am working on a application and I am trying to destroy session on after given time duration and also try to destroy session on browser closing.

Please help me, if it is possible.

Thanks & Regards
Rahul

What I have tried:

I am trying to destroy session on after given time duration and also try to destroy session on browser closing.
Posted
Updated 15-Dec-17 2:45am

1 solution

You can't destroy the session when the browser closes as the internet is stateless, it doesn't know if the browser is open or not and no events you can use are raised when the browser is closed. If you have googled this incredibly frequently asked question you could have easily found this out yourself.

As for destroying it after time, it does that anyway on a sliding expiration. If you want to lessen the time the Session is arive then you can change the default timeout in the web.config file, again google for specifics.

If you want it to be destroyed after an absolute amount of time you'll need to code that yourself. When the Session starts store the DateTime it started in a Session variable

Session["Started"] = DateTime.Now


you could then write a global action filter that checks the difference between the session starting and the current time and if it has passed a threshold to abandon the session. Google for how to implement an global action filter for specifics.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900