使用ajax在MVC 2中处理会话过期

发布于 2024-12-24 00:39:50 字数 229 浏览 4 评论 0原文

我想检查会话是否过期。 所以我决定创建一个名为 IsServerExpired 的操作,并让它返回一个包含布尔值和重定向 url 的 json 对象。

所以java脚本函数将以指定的时间间隔对此操作执行ajax请求.. 我有一些基本问题..

1.如果我发送ajax请求,我认为这会刷新会话时间。因此,实际上,如果我使用此方法,会话将不会过期。我说得对吗?

如果刷新,我如何使用轮询检查会话过期

I want to check the session expired or not.
SO what i decided is Create an action called IsServerExpired and have it return a json object containing a boolean value, and the redirect url.

SO the java script function will do an ajax request to this action with specified time interval ..
I have some basic questions ..

1.If i send an ajax request ,i think that will refresh the session time . So in effect the session will not expire if i am using this method. am i right ?

If it refreshes how can i check session expire using polling

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

近箐 2024-12-31 00:39:51

我认为您对 ASP.NET 会话和身份验证 cookie 感到困惑。我怀疑你在这里谈论的是身份验证 cookie 过期。如果您在 web.config 中将 slidingExpiration 设置为 true,那么轮询 AJAX 请求将更新超时,因此它们不适合。 Phil Haack 描述了一种非常优雅的方法来检测 AJAX 调用中的身份验证 cookie 过期 此博文

I think you are confusing between an ASP.NET session and the authentication cookie. I suspect that you are talking about the authentication cookie expiration here. If you have set slidingExpiration to true in your web.config then polling AJAX requests will renew the timeout so they are not suitable. Phil Haack described a very elegant way to detect authentication cookie expiration in AJAX calls in this blog post.

独夜无伴 2024-12-31 00:39:50

有更简单的方法可以在会话过期后注销用户。

您可以将 SessionTimeout 保存在客户端的某个位置并运行客户端计时器,一旦计时器到达终点,就会重定向用户以注销 url。

这是一个例子。这里的 Model 包含 SessionTimeout 值。

$(document).ready(function () {
  var timeOutInMinutes = @Model;
  if(timeOutInMinutes > 0)
  {
    setTimeout(function() {
      window.location = 
              '@Url.Action("Logout", "Authentication", new {area=""})';   
    },timeOutInMinutes * 1000 * 60);
  }
});

更用户友好的方式是显示弹出窗口,提示会话将在一分钟内过期(如果会话超时 15 分钟,则在 14 分钟后显示),以便用户能够刷新页面。并继续工作。

There is more simple approach to log out user once session expired.

You can save SessionTimeout somewhere on the client side and run client side timer, once timer reach end redirect user to log out url.

Here is example. Model here containts SessionTimeout value.

$(document).ready(function () {
  var timeOutInMinutes = @Model;
  if(timeOutInMinutes > 0)
  {
    setTimeout(function() {
      window.location = 
              '@Url.Action("Logout", "Authentication", new {area=""})';   
    },timeOutInMinutes * 1000 * 60);
  }
});

More user friendly way is to show popup that will say that session will be expired wihtin one minute(if session timeout 15 mins then show it after 14 mins), so user will be able refresh page. and continue work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文