views:

44

answers:

4

i want to create a global session which will stay active until and unless we manually kill it. how to do this in asp.net with c# what i am doing is

HttpContext.Current.Session["UserID"] = someValue;

but in this way the session is lost after some time.

A: 

Store the data in the Application state rather. It will stay there till you remove it, or the app dies/recycles/ends.

Usage:

HttpContext.Current.Application["Foo"] = "bar";
leppie
can you show me some example(code snippets)
Mac
@Mac: Editing answer.
leppie
A: 

You can set the timeout in web.config under system.web -> sessionState -> timeout. Not sure if you can have an infinite session though.

Also, you might be interested in the Application object which stores things in the "application's session" instead of the user's. Comes to my mind because you speak of a "global" session.

What's the application for this? Sounds like you're actually trying to use the session as a persistent storage, which will however only seemingly work even if you manage to set timeout to never or 5 years or whatever - because sessions will be "timed out" once the application is restarted. You might still get around that, but you might be better off looking for real persistence solution like a database. I may be totally off guessing your application for that of course.

Nicolas78
actually i am creating an login page in which after verification i am creating session of that user and signing him/her in.
Mac
OK. Then what happens is simply that the user is logged out after some time. If that time's too short, check the setting in the web.config I mentioned above, and you should be fine! (It's a number in minutes)
Nicolas78
A: 

As nicolas78 says use session timeout configuration property to control the session expiry after user inactivity. In case, you are facing a requirement where session should be active as long as browser is open, there are two ways -

  1. Use cookie to store some token and then re-construct your session state using the token if session gets expired. For example, user details can be recovered from user store if user id is stored in token. At worst, you may have to move your entire state to the database.
  2. Keep the ASP.NET session state but keep it alive by firing AJAX requests from browser. I would suggest to fire such request after n/3 interval where n is your session timeout (ensuring at least three requests are made so even if two gets lost or falls on edges, one gets through).
VinayC
A: 

Perhaps you're after profiles?

http://msdn.microsoft.com/en-us/library/2y3fs9xs.aspx

Profiles live beyond the session and are usually used to store per-user settings that the user can edit, such as their contact details and application preferences.

Profiles can be used with both anonymous and authenticated users. When an anonymous user signs in, their anonymous profile can be migrated into an authenticated profile (i.e. one that is attached to their user name).

Good walkthrough here: http://quickstarts.asp.net/quickstartv20/aspnet/doc/profile/default.aspx

Richard Poole