【问题标题】:clearing session in cache and browser at same time同时清除缓存和浏览器中的会话
【发布时间】:2013-06-07 11:29:55
【问题描述】:

我想禁用多重登录到我的网站。

所以我正在缓存中创建一个会话。

并且我在登录之前检查会话是否已经存在于缓存中,如果不是,我正在创建一个会话并允许他登录。 如果会话已经存在,我将抛出一条错误消息,表明您已在其他地方登录。

我正在注销中清除本地和缓存会话。一切正常,但是当页面因不活动而过期时,本地会话将被删除,但缓存中的会话保持不变。所以即使我尝试从同一个浏览器登录,它也会给出错误,因为我已经登录了。

这是我的代码 在 global.asax 中

 if (HttpContext.Current.Session != null &&
 HttpContext.Current.Session["MULTIPLELOGIN"] != null)
    {
        string sKey = Session["MULTIPLELOGIN"].ToString();
        string sRecruiter = HttpContext.Current.Cache[sKey].ToString();
    }  

在登录中

string sKey = oRecruiter.RecruiterId.ToString();
string sRecruiter = Convert.ToString(HttpContext.Current.Cache[sKey]);
if (sRecruiter == null || sRecruiter == String.Empty)
{
TimeSpan SessTimeOut = new TimeSpan(0, 0, HttpContext.Current.Session.Timeout, 0, 0);
HttpContext.Current.Cache.Insert(sKey, sKey, null, DateTime.MaxValue, SessTimeOut,   System.Web.Caching.CacheItemPriority.NotRemovable, null);
HttpContext.Current.Session["MULTIPLELOGIN"] = userid;

允许登录 } 别的 { 或错误信息 }

请告诉我如何同时使这两个东西过期。

【问题讨论】:

    标签: c# asp.net session caching login


    【解决方案1】:

    给缓存过期超时和会话超时相同,这可能会解决您的问题

    【讨论】:

    • 会话可以保持活动状态,此时超时重置。您也需要重置缓存超时
    • 目前我两个都一样
    【解决方案2】:

    我找到了答案 将 cacheitempriority 从 NotRemovable 更改为 SessTimeOut, System.Web.Caching.CacheItemPriority.Normal

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-20
      • 1970-01-01
      • 1970-01-01
      • 2014-04-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多