1

I have a custom ASP.NET session manager class as follows:

public sealed class SessionManager : Page
    {
        private const string USER = "User";

        private static readonly SessionManager _instance = new SessionManager();

        private SessionManager()
        {
        }


        public static SessionManager Instance
        {
            get { return _instance; }
        }


        public User User
        {
            get { return Session[USER] as User; }
            set { Session[USER] = value; }
        }

    }

After a user successfully logs into the system his full name is displayed on the Home page. Below is the problem I am facing:

1) I first log into the system as John and John's full name is displayed on the homepage and the corresponding user object is kept in session.

2) Now from a different PC I login to the system as Mark. Now Mark's full name is displayed on the homepage and the corresponding user object is kept in session.

3) Now when I go to the PC from which John has logged in and refresh the homepage I see Marks name instead of Johns name.

What could be the reason for this strange behavior?

1 Answer 1

2

Your class inherits from System.Web.UI.Page, which sets its Session member to HttpContext.Current.Session on first access. Since you have a singleton, it will always refer to the first session in which the User property was accessed.

You probably should do something like this:

public sealed class SessionManager
{
    private const string USER = "User";

    private static readonly SessionManager _instance = new SessionManager();

    private SessionManager()
    {
    }

    public static SessionManager Instance
    {
        get { return _instance; }
    }

    public User User
    {
        get { return HttpContext.Current.Session[USER] as User; }
        set { HttpContext.Current.Session[USER] = value; }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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.