Sreedhar Danturthi
Sreedhar Danturthi

Reputation: 7571

query regarding sessions in asp.net

In my web application I am using sessions for storing the user specific data for persisting the data in between the postbacks.

I would like to know the difference between storing the data in sessions as

Session["selectedItem"] = somevalue;

and

Session["UserName"]["SelectedItem"] = somevalue;

where I have a session named Session["UserName"] which stores the name of the user who is logged in.

If i just go into more depth lets say if there are 2 users one logs into firefox and other internet explorer, will there be any conflict if i store the value in the first way meaning the session data is overwritten or shared, and this conflict would be resolved if i use sessions in second way.

Is there any noticeable difference in the way session variable is stored between these 2 session implementations or are they just identical ?

Upvotes: 1

Views: 317

Answers (4)

Øyvind
Øyvind

Reputation: 1610

Data stored in a session is, per definition, stored against a specific user - and it will work regardless of whether your user has been authenticated or not (if your user is anonymous the server will still set a cookie in the user's browser with a unique id for the user's session).

The session object provides a simple one-dimensional collection for storing data, meaning that you can only store data in the session by providing a single key, e.g.

Session["myKey"] = myObject;

Of course, if myObject is an array or another collection then you can reference elements within myObject like this:

Session["myKey"][0];
Session["myKey"]["anotherKey"];

Upvotes: 4

santosh singh
santosh singh

Reputation: 28642

Hope following points clear your doubts

  1. You will get a new session/session ID when using different browsers
  2. If you are using the same browser (tabs or multiple instances) and your session is set to use cookies, by default cookies will be shared among all tabs and/or instances of a browser
  3. If you want a different session when using the same browser you will need to use cookieless sessions.

Upvotes: 0

Talljoe
Talljoe

Reputation: 14827

The session is generally tied to a particular browser through cookies and is isolated from other sessions.

Upvotes: 0

Joel C
Joel C

Reputation: 5567

Sessions are unique per user, so there's no need to key your Session variables by user.

Upvotes: 3

Related Questions