views:

520

answers:

5

I need to keep the session live unless until the user clicks logout in my asp.net mvc(C#) application.

When the user closes the browser and opens again, the session should continue with the values.

I am trying to implement as in stackoverflow.

Any ideas/suggestions?

+6  A: 

You say you want to keep the session alive "as in StackOverflow."... StackOverflow, like most secure sites, does not keep sessions alive indefinitely. It uses cookies to "remember" the login.

Dave Swersky
Is there any sample code for reference?
Prasad
A: 

The session will be lose when the browser is closed

xiangzi
http doesn't have a "session" your server "remembers" a "session" because you asked the "browser" to send something that you used to store state. "something" might be a cookie, a clever way to build URLs, or possibly a hidden field if every "page" is a "form"
No Refunds No Returns
A: 

If you want to remember 'state' even when (because of the expired session / session cookie) you are forcing your users to login again. You need to persist the session data. Perhaps your web-container can do this for you.

Stefan Hendriks
A: 

First, if you want to make multi-session but temp data, you should probably look into the ASP.NET user profile.

If you want to persist logins across sessions, look at the bits of FormsAuthentication that deal with remembering the user.

If you need to keep sessions alive indefinitely without setting the timeout forever (therefor triggering murder by the server admin in some cases), a neat trick is to setup an Ajax "heartbeat" to ping back to the server while the browser is open and effectively do a "keep this session alive" trick.

Wyatt Barnett
+1  A: 

if you use FormsAuthentication, you can do something like:

FormsAuthentication.SetAuthCookie("userName", true);

That will create a cookie that is persisted across different browser sessions, and will achieve what you're looking for.

oz