0
我們有這個助手類來訪問會話,但它只存儲最後一個值。我無法檢測到任何錯誤。SessionHelper類只存儲最後一個用戶
SessionHelper.Instance.AddToSession(SessionConstants.CURRENT_USER_INFO, user);
我認爲初始化類的字典部分造成問題,但我無法找出它:
public interface ISessionHelper
{
void AddToSession<T>(string key, T value);
T GetFromSession<T>(string key);
void RemoveFromSession(string key);
}
public class SessionHelper : ISessionHelper
{
private static SessionHelper sessionHelper;
private Dictionary<string, object> sessionCollection;
private const string SESSION_MASTER_KEY = "SESSION_MASTER";
private SessionHelper()
{
if (null == sessionCollection)
{
sessionCollection = new Dictionary<string, object>();
}
}
public static ISessionHelper Instance
{
get
{
if (null == sessionHelper)
{
sessionHelper = new SessionHelper();
}
return sessionHelper;
}
}
public void AddToSession<T>(string key, T value)
{
sessionCollection[key] = value;
HttpContext.Current.Session.Add(SESSION_MASTER_KEY, sessionCollection);
}
public void RemoveFromSession(string key)
{
sessionCollection.Remove(key);
}
public T GetFromSession<T>(string key)
{
var sessionCollection = HttpContext.Current.Session[SESSION_MASTER_KEY] as Dictionary<string, object>;
if (null != sessionCollection)
{
if (sessionCollection.ContainsKey(key))
{
return (T)sessionCollection[key];
}
else
{
return default(T);
}
}
else
{
throw new Exception("Session Abandoned");
}
}
}
這一類的調用將按照如下處理。
或者這可能是問題嗎?
void Session_Start(object sender, EventArgs e)
{
CUserRoles userRoles;
string currentUser = HttpContext.Current.User.Identity.Name;
User user = UserManager.GetUserCompleteInfo(currentUser, out userRoles);
if (user != null && user.UserID > 0)
{
SessionHelper.Instance.AddToSession(SessionConstants.CURRENT_USER_INFO, user);
if (userRoles != null)
{
SessionHelper.Instance.AddToSession(SessionConstants.CURRENT_USER_ROLE, userRoles);
}
}
else
{
Response.Redirect("AccessDenied.aspx", true);
}
}
請提供代碼段。我沒有得到你想說的話。 – Charlie 2013-03-27 16:24:31
@Charlie我已經發布了一個。 – 2013-03-27 16:25:10
但是這不會導致AddToSession()方法的更改嗎? – Charlie 2013-03-27 16:29:25