Nibirue
Nibirue

Reputation: 411

Creating and storing a session

I'm working on creating a session for a user login on my website. I can initialize the session and use its members just fine, but I also need a method within my session class that will store itself. I need to provide HttpSessionState as an input parameter, and then store it into an object like: Session["sessionName"]=this;.

Furthermore, when I want to retrieve the session, it won't yet be created, so it must be static. Then I need to return a new instance of my session class with the properties filled (username and companyID) out of the HttpSessionState.

How can this be done in my session class? What I've described above is from the research I've done that provides a particular solution to my problem, but since I'm new to using session, I don't quite understand it. Thanks.

Snippet of my session class:

public class MySession : System.Web.UI.Page
    {
        private MySession()
        {
            Username    = Business.User.labelUsername;
            CompanyId   = Business.User.labelCompanyId;
        }

        public static MySession Current
        {
            get
            {
                try
                {
                    MySession session = (MySession)HttpContext.Current.Session["sessionName"];

                    if (session == null)
                    {
                        session = new MySession();
                        HttpContext.Current.Session["sessionName"]=session;
                    }
                    return session;
                }
                catch (NullReferenceException e)
                {
                    Debug.WriteLine("NullReferenceException:");
                    Debug.WriteLine(e);
                }
                return null;
            }
        }

        public string Username
        {
            get; set;
        }

        public string CompanyId
        {
            get; set;
        }
    }

Upvotes: 0

Views: 534

Answers (2)

Mun
Mun

Reputation: 14308

You could try using a serialized "session info" object:

[Serializable]
public class SessionInfo
{
    // Stuff to store in session
    public string Name { get; set; }
    public int Foo { get; set; }

    private SessionInfo()
    {
        // Constructor, set any defaults here
        Name = ""
        Foo = 10;
    }

    public static SessionInfo Current
    {
        get
        {
            // Try get session info from session
            var info = HttpContext.Current.Session["SessionInfo"] as SessionInfo;

            // Not found in session, so create and store new session info
            if (info == null)
            {
                info = new SessionInfo();
                HttpContext.Current.Session["SessionInfo"] = info;
            }

            return info;
        }
    }
}

You can then use this from within your application like this:

SessionInfo.Current.Name = "Something Here";
SessionInfo.Current.Foo = 100;

The serialization/deserialization is all done within the SessionInfo object, and you get the benefit of type safe data.

Upvotes: 3

ChrisLively
ChrisLively

Reputation: 88044

What you are asking about is called serialization and deserialization.

Serialization is taking an object and converting it to a format, such as a string, that can be stored. Deserialization is the reverse of that action.

The "quick" way is to add the [Serializable] attribute to your class. However, without knowing the details of that class it's hard to say whether it is in fact easily serializable without a bit of work.

Here's a walkthrough: http://msdn.microsoft.com/en-us/library/vstudio/et91as27.aspx

Upvotes: 1

Related Questions