用户关闭浏览器后如何终止会话? [英] How to kill the session as soon as user closes the browser?

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

问题描述

我的应用程序在Windows身份验证上工作,当用户关闭窗口时如何终止会话?

Hi, My application works on windows authentication, how to kill the session when user closes the window?

推荐答案

简短地说:您不能.

由于Web应用程序使用http,并且http是无状态的,因此您无法确定客户端将发生什么,因为客户端和服务器之间没有连接.唯一可以用来跟踪它的是会话.只要已将会话设置为保留会话,或者应用程序正在关闭会话,会话就会由服务器保留.
因此,如果要关闭会话,则必须从服务器启动它.因此,必须通知应用程序的服务器端有关浏览器关闭的信息.在客户端删除会话cookie不会在服务器端关闭会话.
您可能会尝试捕获诸如离开站点,关闭浏览器窗口之类的事件,但这仅是用户离开应用程序的部分情况:可能会断开网络连接,杀死浏览器,浏览器可能崩溃等.这些事件将永远不会被您的应用程序捕获.因此,您不能依赖客户端.
尽管可以使用正常事件,但是请注意,不能保证这些事件将到达服务器端.阅读本文:浏览器的窗口关闭事件 [
Shortly: you can not.

As web applications use http, and http is stateless, you can not know for sure what happens on client side, since there is no connection between client and server. The only thing that can be used to keep track of it is the session. And the session is kept by the server as long as it was set up to be kept, or the application is closing it.
So if you want to close the session, you have to initiate it from the server. Thus the server side part of your application has to be notified about browser closure. Deleting the session cookie on client side won''t close the session on server side.
You might try to catch events like leaving your site, closing a browser window, but this is only part of the situations how the user could leave your application: might loose network connection, kill the browser, browser might crash, and so on. These event will be never caught by your application. Thus you can not rely on client side.
Although you can use the normal events, but be aware, that there is no guarantee, that these will arrive on server side. Read this article: Window Close Event of Browser[^]
What you also can do:
- optimize session timeout
- give a countdown timer to the user and warn him, if time is running out (and do an ajax postback if user wants to continue)
- do automatic keepalive postbacks (be careful not to use this if slow or expensive connection is in place)
- encourage the user to log out


阅读

您可以创建一个asmx Web服务文件,并在其中放置一个类,以类似于这个:
You can create an asmx web service file and put a class in it simliar to this:
[System.Web.Script.Services.ScriptService]
public class Clearing : System.Web.Services.WebService
{
        [WebMethod(EnableSession = true)]
        public bool KillSession()
        {
                HttpContext.Current.Session.Clear();
                return true;


        }
}


然后,在您的页面上


Then, on your page

<asp:ScriptManagerProxy ID="smp1" runat="server">
                <Services>
                        <asp:ServiceReference Path="NameOfYourAsmxFile.asmx" />
                </Services>
        </asp:ScriptManagerProxy>


最后,使用onunload函数中的javascript:


And finally, in javascript from your onunload function:

ret = Clearing.KillSession(OnKillSessionComplete, OnServiceError, OnServiceError);


这篇关于用户关闭浏览器后如何终止会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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