0

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.

4
  • Many answers are recommending to use Session. Based on your description that would be the correct solution. But you haven't said why you need to store the cookie. Why? Commented Aug 16, 2012 at 17:45
  • I need to have the username for logging purposes and to output to the screen as a logged in user. Commented Aug 16, 2012 at 19:47
  • Ok, then it might be slightly easier to get it from the Session as compared to always getting it from the cookie. I do think you'd do well to follow Darin Dimitrov's advice and check out Forms Authentication. Using that, the user name of the current user (after logging in, of course) is easily accessible: string userName1 = Page.User.Identity.Name; or string userName2 = HttpContext.Current.User.Identity.Name;. The forms authentication module takes care of the cookie handling for you. Commented Aug 16, 2012 at 20:00
  • Does this work if we use something like SecurID for logging in and authentication? Commented Aug 16, 2012 at 20:04

4 Answers 4

1

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();
Sign up to request clarification or add additional context in comments.

2 Comments

Is Session["username"] != Session["username"] when they are in different sessions/processes? For example, if userA logs in I need to store userA and his IP address and now userB logs in and I store his username and IP address. Do I just set both to Session["username"] AND Session["ipaddress"]?
You will have unique copy of Session["username"] for each user. Suppose for one user you store John by Session["username"] = "John" and for other you store Session["username"]="Peter" then you will have two different session copies for each user. This may help you msdn.microsoft.com/en-us/library/ms972429.aspx
0

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.

Comments

0

Can't you just use Session?

Session["username"] = username;


var username = Session["username"];

Comments

0

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.

2 Comments

Just curious, how would you go about this problem IF you DID have huge amounts of data for each session?
I've never done it personally, but there is an option to compress session data. Additionally, you can set up an asp.net application to store session data out of the main process and even on a separate box. Ideally though, session should be used for smaller chunks of data if at all possible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.