Static Session Class and Multiple Users

I am creating a class to store the user ID and user role in a session. I am not sure how this class will behave when several users are located at the same time. Does anyone see a problem with this?

public static class SessionHandler { //*** Session String Values *********************** private static string _userID = "UserID"; private static string _userRole = "UserRole"; //*** Sets and Gets ********************************************************** public static string UserID { get { if (HttpContext.Current.Session[SessionHandler._userID] == null) { return string.Empty; } else { return HttpContext.Current.Session[SessionHandler._userID].ToString(); } } set { HttpContext.Current.Session[SessionHandler._userID] = value; } } public static string UserRole { get { if (HttpContext.Current.Session[SessionHandler._userRole] == null) { return string.Empty; } else { return HttpContext.Current.Session[SessionHandler._userRole].ToString(); } } set { HttpContext.Current.Session[SessionHandler._userRole] = value; } } } 
+7
c # session
source share
3 answers

The code you sent is an exact copy of some of the code that we have here.

It has been working fine for 2 years now.

Each user access is its own session. Each request made to the server represents a new thread. Although there are two requests at the same time, HttpContext.Current is different for each of these requests.

+6
source share

You will receive a new session for each connection. None of the two users will use the session. Each connection will have its own SessionID value. As long as the user remains on your page (does not close the browser, etc.), the User will save this session from one request to the next.

+3
source share

This will work great for users with multiple users accessing your application, as there will be a different sessionid generated for all deffrent users accessing the application at the same time. It will work in a similar way if you define two different session variables on your system. This will be similar to the state of a buffer towing session using the static SessionHandler wrapper class.

0
source share

All Articles