C# MVC 使用 jquery 创建基于计时器的自动注销?

发布于 2024-11-14 10:45:57 字数 242 浏览 0 评论 0 原文

如何创建 jquery 脚本以在用户处于不活动状态一段时间后自动注销?或者有没有jquery插件可以做到这一点?需要执行以下操作:

  1. 使用 setTimeout 创建计时器(例如 30 分钟)
  2. 每次用户与页面交互时重置
  3. 超时 setTimeout 到期后,使用 ajax 调用服务器上的 /logout 操作(asp.net mvc )
  4. 显示模式/灯箱对话框,告诉用户再次登录

How do I create an jquery script to automatically log out the user after a set period of inactivity? Or is there a jquery plugin that does this? It would need to do something like:

  1. Create timer with setTimeout (eg for 30 mins)
  2. Reset the timeout every time the user interacts with the page
  3. After the setTimeout expires, use ajax to call the /logout action on the server (asp.net mvc)
  4. Show a modal/lightbox dialog telling the user to login again

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

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

发布评论

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

评论(2

土豪 2024-11-21 10:45:57

您需要在这里考虑几个不同的方面。首先,如果用户关闭浏览器或者用户使用的计算机死机了,会发生什么?如果用户在 30 分钟内访问该页面,该人是否仍应登录?

假设用户无论如何都应该登录 30 分钟。最简单的开始方法是在身份验证 cookie 上设置 cookie 超时。请记住在每个页面刷新时更新超时。并使用 jQuery 计时器来检查 cookie 是否仍然有效,或者只是跟踪用户的登录时间。

因此,jQuery 计时器,您可以在每个页面加载刷新 cookie,然后检查计时器是否/delay执行,如果有,则移除cookie并显示模态框。

有很多方法可以做到这一点,使用计时器是一种方法。

$(this).oneTime(1800 , function() {
    location.href='/logout'; // redirects to logout page.
});

另一种方法是使用服务器端检查,但你不会得到模型框,正如我所说,有很多方法,这完全取决于你的喜好。

You have couple of different aspects that you need to consider here. First of all, what happens if the user just closes the browser or if the computer the person is using dies? If the user visits the page within 30 minutes, should the person still be logged in?

Let's say that the user should be logged in for 30 minutes, no matter what. The easiest way to start is to set a cookie timeout on an authentication cookie. Remember to update the timeout when each page refreshes. And use a jQuery timer to check if the cookie is still valid or not, or just keep track of the users login time.

So, jQuery timers, you could on each page load refresh the cookie and then just check if the timer/delay executes, if so, remove the cookie and display a modal box.

There's tons of ways of doing this, using the timers are one way.

$(this).oneTime(1800 , function() {
    location.href='/logout'; // redirects to logout page.
});

Another aproach is to use server side checking for this, but you will not get the model box for this, as I said, there are tons of ways, it all depends on your preferences.

百思不得你姐 2024-11-21 10:45:57

我知道这是一个非常古老的问题,但对于寻找类似解决方案的人来说可能很有用,如果用户在特定时间段内处于空闲状态

http://www.dotnetfunda.com/articles/show/3130/automatically-logout-when-user-is-idle-for-sometime

该链接可能会消失,所以这里是代码-

<script>
    $(function () {
        $("body").on('click keypress', function () {
            ResetThisSession();
        });
    });



var timeInSecondsAfterSessionOut = 30; // change this to change session time out (in seconds).
    var secondTick = 0;

    function ResetThisSession() {
        secondTick = 0;
    }

    function StartThisSessionTimer() {
        secondTick++;
        var timeLeft = ((timeInSecondsAfterSessionOut - secondTick) / 60).toFixed(0); // in minutes
    timeLeft = timeInSecondsAfterSessionOut - secondTick; // override, we have 30 secs only 

        $("#spanTimeLeft").html(timeLeft);

        if (secondTick > timeInSecondsAfterSessionOut) {
            clearTimeout(tick);
            window.location = "/Logout.aspx";
            return;
        }
        tick = setTimeout("StartThisSessionTimer()", 1000);
    }

    StartThisSessionTimer();

I know it's very old question, but could be useful for someone looking for similar solution to log out user if they are idle for certain timeperiod

http://www.dotnetfunda.com/articles/show/3130/automatically-logout-when-user-is-idle-for-sometime

The link may disappear so here is the code -

<script>
    $(function () {
        $("body").on('click keypress', function () {
            ResetThisSession();
        });
    });



var timeInSecondsAfterSessionOut = 30; // change this to change session time out (in seconds).
    var secondTick = 0;

    function ResetThisSession() {
        secondTick = 0;
    }

    function StartThisSessionTimer() {
        secondTick++;
        var timeLeft = ((timeInSecondsAfterSessionOut - secondTick) / 60).toFixed(0); // in minutes
    timeLeft = timeInSecondsAfterSessionOut - secondTick; // override, we have 30 secs only 

        $("#spanTimeLeft").html(timeLeft);

        if (secondTick > timeInSecondsAfterSessionOut) {
            clearTimeout(tick);
            window.location = "/Logout.aspx";
            return;
        }
        tick = setTimeout("StartThisSessionTimer()", 1000);
    }

    StartThisSessionTimer();

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