我有一个用户在两个浏览器中使用一个页面。
我使用session来存储一些对象。
我应该在两个浏览器中获得此对象。
但是CurrentSession是用不同的键创建的。如何使用SQL Server SessionState机制为一个用户和存储创建不同浏览器的会话?
答案 0 :(得分:1)
您可以使用全局缓存对象来保存用户项而不是Session。如果滑动到期时间等于Session.Timeout
持续时间,则其行为应类似于会话。
public void AddToCrossBrowserSession(string username, string key, object value)
{
string cacheKey = string.Format("Cross-browser-state:{0}/{1}", username, key);
System.Web.HttpContext.Current.Cache.Add(cacheKey, value, null, System.Web.Caching.Cache.NoAbsoluteExpiration, System.TimeSpan.FromMinutes(Session.Timeout), System.Web.Caching.CacheItemPriority.Normal, null);
}
public object GetFromCrossBrowserSession(string username, string key)
{
string cacheKey = string.Format("Cross-browser-state:{0}/{1}", username, key);
return System.Web.HttpContext.Current.Cache[cacheKey];
}