用户关闭浏览器时如何自动注销 [英] How to do automatic logout when user closed the browser

查看:260
本文介绍了用户关闭浏览器时如何自动注销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果用户关闭了Web浏览器,则会自动删除会话历史记录,但是我必须在我的数据库中更新它是否仍然在会话中

If user closed the web browser then automatically session history is erased, but I have to update it in my database whether user still in the session or not

推荐答案

在您的布局中,您应该使用Javascript,如下一个示例;



In your layout you should use Javascript like in the next example;

<script type="text/javascript">
        

        function onWindowClosing() {
            if (window.event.clientX < 0 || window.event.clientY < 0) {


.ajax({
type:POST,
url:/ Account / OnWindowClosing
});
}
};

函数onKeydown(evt){
if(evt!= undefined&& evt.altKey&& evt.keyCode == 115)// Alt + F4
{
.ajax({ type: "POST", url: "/Account/OnWindowClosing" }); } }; function onKeydown(evt) { if (evt != undefined && evt.altKey && evt.keyCode == 115) //Alt + F4 {


.ajax({
type:POST,
url:/ Account / OnWindowClosing
});
}
};

window.onbeforeunload = onWindowClosing;
window.document.onkeydown = onKeydown;

< / script>
.ajax({ type: "POST", url: "/Account/OnWindowClosing" }); } }; window.onbeforeunload = onWindowClosing; window.document.onkeydown = onKeydown; </script>





然后在您的帐户控制器你必须实现方法 OnWindowClosing 来保存你想要的东西。



在我的下一篇文章中: MVC基本站点:第4步 - 使用AJAX,JSON在MVC 4.0中进行jqGrid集成, jQuery,LINQ和序列化 [ ^ ]我还提供了源代码,您可以看到上述事件的完整实现。尤其要看 _Layout.cshtml 布局页面和 AccountController 类。



Then in your Account controller you have to implement the method OnWindowClosing to save what you want.

In my next article: MVC Basic Site: Step 4 – jqGrid Integration in MVC 4.0 using AJAX, JSON, jQuery, LINQ, and Serialization[^] I have provided also source code, and you can see the complete implementation of the above events. Have a look especially on _Layout.cshtml layout page and on AccountController class.


这篇关于用户关闭浏览器时如何自动注销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆