Я использовал собственный класс SessionWrapper для обработки Session в моем проекте ASP.NET.Кажется, все в порядке, но я недавно обнаружил, что в нем есть смешная ошибка.Если многие пользователи войдут в мой сайт, их сессия будет обменена или смешана.У пользователя A будет сеанс пользователя B, и, возможно, у пользователя C будет сеанс пользователя A. Он может измениться после обновления пользователем страницы.
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;
}
}
}