I am trying to create a class to handle session management but can't seem to get the right syntax to retrieve the session variables from the class. I have this class:
And in my Master Page.cs I have this code to set the session variables:
// Authenticate User and assing authorization Ids
User myUser =
UserManager.GetUser();
// Set Session variables
SessionManager mySession =
new SessionManager(Session);
mySession.UserTypeId = myUser.UserTypeId;
In my default.cs page I want to print the UsertypeId session variable to the screen. This give me 0 but if I refresh the page it gives me the right UserTypeId.
SessionManager mySession =
new SessionManager();
Response.Write(mySession.UserTypeId);
I think it has to do with me instantiating a new session? How would I get the stored session variable?
thanks for the reply but i think that is much more than i care to implement. i am using sql server to store session variables. i'm just trying to get the values from the object once they are stored but can't seem to do so.
jrp210
Member
20 Points
108 Posts
Session Manager Class
Feb 14, 2008 05:33 PM|LINK
I am trying to create a class to handle session management but can't seem to get the right syntax to retrieve the session variables from the class. I have this class:
public class SessionManager{
private HttpSessionState session_;public SessionManager(HttpSessionState session){
session_ = session;
}
public int UserTypeId{
get { return session_["UserTypeId"] != null ? Convert.ToInt32(session_["UserTypeId"]) : 0; }set { session_["UserTypeId"] = value; }}
}
And in my Master Page.cs I have this code to set the session variables:
// Authenticate User and assing authorization Ids User myUser = UserManager.GetUser(); // Set Session variables SessionManager mySession = new SessionManager(Session);mySession.UserTypeId = myUser.UserTypeId;
In my default.cs page I want to print the UsertypeId session variable to the screen. This give me 0 but if I refresh the page it gives me the right UserTypeId.
SessionManager mySession = new SessionManager();Response.Write(mySession.UserTypeId);
I think it has to do with me instantiating a new session? How would I get the stored session variable?
Thanks
bcanonica
Contributor
3082 Points
595 Posts
Re: Session Manager Class
Feb 14, 2008 05:50 PM|LINK
This article should help you do what you are trying to do.
http://www.c-sharpcorner.com/UploadFile/karthik_mascon/CreatingCustomSessionMgmt11262005063553AM/CreatingCustomSessionMgmt.aspx
Thanks,
BC
Blog Void Impossible
jrp210
Member
20 Points
108 Posts
Re: Session Manager Class
Feb 14, 2008 06:56 PM|LINK
thanks for the reply but i think that is much more than i care to implement. i am using sql server to store session variables. i'm just trying to get the values from the object once they are stored but can't seem to do so.
jrp210
Member
20 Points
108 Posts
Re: Session Manager Class
Feb 15, 2008 05:56 PM|LINK
I found out how to do it. Was missing the static keyword!
mrrogers
Member
202 Points
728 Posts
Re: Session Manager Class
Nov 19, 2012 02:20 PM|LINK
----