user310291
user310291

Reputation: 38180

Is static variable in asp.net common to all users?

I would like to have kind of static variable but isolated for each user session so is static variable in asp.net common to all users ?

If yes is there an easy to emulate what I want without using user session explicitly ? I cannot use Viewstate either as I'm using MVC (or am I mistaken ?).

Upvotes: 1

Views: 2357

Answers (2)

Ann L.
Ann L.

Reputation: 13965

Anything static is common across ASP.NET, not localized to sessions.

I'll leave it to others to come up with a way to emulate session state without using session state.

Upvotes: 3

Anders Abel
Anders Abel

Reputation: 69260

A static variable will be common for all sessions yes.

If you want to keep per-user-session data you have three options:

  • Use built in Session handling.
  • Set a cookie.
  • Pass the data as a hidden form field that is posted to the server (works for places where you have a form anyway).

The session (reached with Session["someKey"]) is implemented by ASP.NET setting a cookie in the user's browser, that uniquely identifies that session. That id is then used to look up the right set of session data held in the server's memory. The big advantage of using Session is that the data is not visible, nor possible to manipulate for the user. The big disadvantage is that everything is lost when the app pool recycles.

Setting a cookie is more light weight to the server, but the data is visible and can be manipulated by the user. If the data is sensitive, you have to encrypt it. A cookie is easier to keep for longer periods of time.

Passing data as a hidden form field is really just a way to mimic ViewState. It's good in e.g. an update form to pass the id of the record being updated, but not for very much else.

Upvotes: 7

Related Questions