keelerjr12
keelerjr12

Reputation: 1923

ASP.NET Session Management With Variables

Ran into a stupid problem...

So right now I implement Application_Start to load up the hash tables, which works fine. Next in Session_Start I parse a user's cookie to retrieve his name, which, again, works fine. The problem happens when I store his username in a variable in Global.asax.cs. I didn't really realize that this is a variable shared among all processes/threads. So I guess my question is how do you parse a user's cookie one time and then save the data for sure elsewhere in the process/thread.

Upvotes: 0

Views: 144

Answers (4)

Adil
Adil

Reputation: 148170

You can store cookies in session and use it later. You can store any object in session collection and retrieve it later before it expires. You can read more over here

Assigning to session

Session["anyName"] = "value";

Retrieving from session object

string str = Session["anyName"].ToString();

Upvotes: 1

mElling
mElling

Reputation: 197

Session is definitely the way I would go. Having HUGE amounts of data in Session could potentially lead to performance problems, but only storing small strings for each user shouldn't matter at all.

Upvotes: 0

mxmissile
mxmissile

Reputation: 11681

Can't you just use Session?

Session["username"] = username;


var username = Session["username"];

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1039398

That's exactly what the ASP.NET session is meant for. Store this information there. It will be only specific to the current user and not shared among all users.

By the way you might also want to checkout Forms Authentication in ASP.NET, just to make sure that you don't reinvent some wheels.

Upvotes: 0

Related Questions