在ASP.NET中关闭浏览器关闭会话 [英] Remove session on browser close in ASP.NET

查看:273
本文介绍了在ASP.NET中关闭浏览器关闭会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在关闭浏览器时删除特定会话。该项目在asp.net中使用c#。



我尝试过:



I need to remove a specific session on closing the browser. The project is in asp.net with c#.

What I have tried:

I need to remove a specific session on closing the browser. The project is in asp.net with c#.

推荐答案

您不能:浏览器没有通知您的网站浏览器正在关闭,或者您的网站显示的页面已关闭,或者用户已离开您的网站。



即使有,也会发生什么如果用户拉出电源引线?
You can't: there is no notification from the browser to your website that the browser is closing, or the page your site was displayed on has closed, or the user has navigated away from your site.

Even if there were, what would happen if the user pulls the mains lead out?


您不能因为服务器不知道用户已关闭浏览器。您只需要等待会话到期。
You can't because the server doesn't know the user has closed the browser. You'll just need to wait for the session to expire.


没有100%可靠的东西,但有些选项,有些比其他选项更难。



1.设置WebSockets。如果它只用于监视服务器是否与浏览器有活动连接,那么似乎有点矫枉过正。



2. COMET。一种基于AJAX的方法,使用javasript计时器连续ping服务器以创建准长运行的http请求。关闭页面/标签后,它将打破周期,您将知道此会话已关闭。



3. Javascript事件 onunload onbeforeunload 。使用事件触发AJAX帖子通知会话关闭。这些可能需要在帖子后立即启动警报/确认,以便AJAX在请求中止之前完成。



4.意图。那些令人讨厌的javascripts,当鼠标离开浏览区域时会导致模式窗口弹出。这将再次启动AJAX调用。
Nothing that is 100% reliable, but there are options, some more difficult than others.

1. Setting up WebSockets. Seems to be overkill if it's only going to be used for monitoring if the server has an active connection to the browser.

2. COMET. An AJAX based approach using a javasript timer to continuously ping the server to create a quasi long running http request. Once the page/tab is closed it will break the cycle and you will know this session is closed.

3. Javascript events onunload and onbeforeunload. Use the events to trigger an AJAX post notifying of session close. These would probably need to fire up an alert/confirmation immediately after the post so that the AJAX completes before the request is aborted.

4. "Intents". Those annoying javascripts that cause a modal window to pop-up when the mouse leaves the browing area. Again this would fire up an AJAX call.


这篇关于在ASP.NET中关闭浏览器关闭会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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