I used a custom SessionWrapper class for handling Session in my ASP.NET project. It seems ok but I have recently found that it has hilarious error. If many user login to my website, their session will be exchanged or mixed. User A will has session of User B and maybe User C has Session of User A. It may change after User refresh the page.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
namespace MyNamespace
{
public class SessionWrapper
{
public static string USER_SESSION = "userSession";
public static void SetUserSession(string email, Dictionary<int, DateTime> products)
{
UserSession userSession = new UserSession() { Email = email, Products = products };
System.Web.HttpContext.Current.Session.Add(USER_SESSION, userSession);
}
public static UserSession GetUserSession()
{
if (System.Web.HttpContext.Current.Session[USER_SESSION] != null)
{
return System.Web.HttpContext.Current.Session[USER_SESSION] as UserSession;
}
return null;
}
public static void RemoveUserSession()
{
if (System.Web.HttpContext.Current.Session[USER_SESSION] != null)
{
System.Web.HttpContext.Current.Session.Remove(USER_SESSION);
}
}
}
public class UserSession
{
private string email;
private Dictionary<int, DateTime> products = new Dictionary<int, DateTime>();
public string Email
{
get;
set;
}
public Dictionary<int, DateTime> Products
{
get;
set;
}
}
}
The only reason apart from some strange code in application like Jonas H noticed is that you are testing it in either one browser instance in separate tabs or two instances of the same browser.
Try display the HttpContext.Current.Session.SessionID to determine whether you are in the same session in all cases.