如何在MVC中关闭浏览器时注销用户?

问题描述 投票:0回答:2

我创建了一个MVC asp.net项目,我必须在浏览器关闭时保存日期时间。我尝试过之前给出的解决方案。一个示例项目可以帮助我理解它是如何工作的?

html asp.net-mvc model-view-controller
2个回答
1
投票

这样我实现。选项卡关闭时使用onbeforeunload事件。

<script type="text/javascript">
    window.onbeforeunload = function () {
        window.location.href = '/logout'; //your action to logout

    });
  };
</script>

或者ajax调用注销

<script type="text/javascript">
    window.onbeforeunload = function () {
         $.ajax({
        type: "POST",
        url: "/logout",
        success: function (result) {
            //
        }
    });

    };
</script>

0
投票

那么你可以在用户表中添加UserLastLoginDateUserLastVistedDate

然后,下次用户打开页面时,您将使用当前日期时间验证UserLastLoginDate,并且您有关闭brwoser的时间。

© www.soinside.com 2019 - 2024. All rights reserved.