【问题标题】:Session is not getting destroyed MVC3会话没有被破坏 MVC3
【发布时间】:2013-01-02 13:32:08
【问题描述】:

我正在尝试使用此 jquery 函数每 5 秒检查一次会话是否仍然存在

setTimeout(IsSessionAlive, 5000);

 function IsSessionAlive()
 {
    $.getJSON('@Url.Action("IsSessionAlive", "Account")', function (isAlive) {
       if (!isAlive)
         alert(isAlive);
         setTimeout(IsSessionAlive, 5000);
     });
 }

这就是我尝试从服务器获取会话状态的方式

public JsonResult IsSessionAlive()
{
   var isA = Request.IsAuthenticated;
   return Json(Session["LoggedIn"] != null, JsonRequestBehavior.AllowGet);
}

只是为了测试我在 LogOn 上执行此操作的代码

Session["LoggedIn"] = true;
Session.Timeout = 1;

我希望它应该在 1 分钟不活动后销毁会话,当我的 jquery 将检查会话时,它会发现它为空,并将向用户显示您必须再次输入密码的弹出窗口。 但我从来没有发现会话无效! 有什么想法吗?

【问题讨论】:

  • 您的会话将在 1 分钟不活动后过期。但是由于您的原因活动每 5 秒一次,因此这永远不会发生。
  • 哦!会被视为用户活动???
  • 是的,对于网络服务器来说,它是一个和其他请求一样的请求。
  • 哦,好的!那么有没有其他方法可以实现这一点,有 jquery 插件,但它们似乎都有一些错误或限制。所以我认为检查会话将是最安全的。

标签: asp.net-mvc-3 session-timeout


【解决方案1】:

您可以使用jQuery idleTimer plugin 检测空闲超时并在超时发生时显示弹出窗口。

示例代码:

// idleTimer() takes an optional argument that defines the idle timeout
// timeout is in milliseconds; defaults to 30000
$.idleTimer(10000);


$(document).bind("idle.idleTimer", function(){
 // function you want to fire when the user goes idle
});


$(document).bind("active.idleTimer", function(){
 // function you want to fire when the user becomes active again
});

// pass the string 'destroy' to stop the timer
$.idleTimer('destroy');

【讨论】:

  • 太棒了!但是我的问题有什么原因吗?
猜你喜欢
  • 2011-09-19
  • 1970-01-01
  • 1970-01-01
  • 2017-09-11
  • 2013-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多