C# 每当浏览器在 asp.net 中关闭时,是否可以清除会话?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13247848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is this possible to clear the session whenever browser closed in asp.net?
提问by Saravanan
In my asp.net application, i want to clear the session whenever my browser closed or my tab (if my browser containing multiple tabs)closed.
在我的 asp.net 应用程序中,我想在浏览器关闭或选项卡(如果我的浏览器包含多个选项卡)关闭时清除会话。
Please guide me to get out of this issue...
请指导我摆脱这个问题......
采纳答案by PaulMolloy
Short version, No. There's no solid way of a server detecting if the client has closed their browser. It's just the nature of web development's asynchronous pattern.
简短版本,不。没有可靠的方法来检测客户端是否关闭了浏览器。这只是 Web 开发异步模式的本质。
Long version, if it's really, really important to you; Put a bit of javascript in the page that sends a regular post to your website in the background and set up a serverside agent or service that disposes of the sessions if it doesnt receive these regular "heartbeat" signals.
长版,如果它对你真的非常重要;将一些 javascript 放在页面中,在后台向您的网站发送常规帖子,并设置服务器端代理或服务,如果它没有收到这些常规的“心跳”信号,则会处理会话。
You can put a javascript postback onto the page's unload() event but dont rely on it, it doesnt always fire.
您可以在页面的 unload() 事件上放置一个 javascript 回发,但不要依赖它,它并不总是会触发。
回答by Jordi
You can try to do that with javascript. Check it at:
您可以尝试使用 javascript 来做到这一点。检查它:
http://www.codeproject.com/Tips/154801/How-to-end-user-session-when-browser-closed
http://www.codeproject.com/Tips/154801/How-to-end-user-session-when-browser-closed
回答by Asif Mushtaq
I think cookiescan better meet your requirement here for session management.
我认为cookies在这里可以更好地满足您对会话管理的要求。
it means that session data should not be stored on the server and should be with your call, so that you don't have to worry about clearing the data on server.
这意味着会话数据不应该存储在服务器上,而应该与您的通话一起使用,这样您就不必担心清除服务器上的数据。
回答by Lawrence Johnson
This happens by default whenever you close your browser, and that's not just for ASP.NET. It's for most server-side programming languages that have a session state. Basically, any cookie that is added that doesn't specify an expiration date, will be deleted when the browser is closed.
每当您关闭浏览器时,默认情况下都会发生这种情况,这不仅适用于 ASP.NET。它适用于大多数具有会话状态的服务器端编程语言。基本上,任何添加的未指定到期日期的 cookie 都将在浏览器关闭时被删除。
Where this doesn't apply, is when you close a tab, which is something you will not have any control over because the tab close event will not get sent back to the Web server.
这不适用的地方是当您关闭选项卡时,您将无法进行任何控制,因为选项卡关闭事件不会被发送回 Web 服务器。
回答by iShare
You can use Session_End event of Global.aspx
您可以使用 Global.aspx 的 Session_End 事件
//For Specific Session
Session.Remove("SessionName");
//All the Session
Session.Abandon();
回答by Shaz
Alternatively you can check you previous session state on every new browser opening and can Session.clear() or Session.abandon() the previous session.
或者,您可以在每次打开新浏览器时检查之前的会话状态,并且可以 Session.clear() 或 Session.abandon() 上一个会话。
this will make sure that every time you start application you will get new session.
这将确保每次启动应用程序时都会获得新会话。
- use BasePage in your .net application.
- Check the session.sessionid on basepage load.
- More Inforamtion how to detect new session in basepage. BasePage.Session.Link
- 在您的 .net 应用程序中使用 BasePage。
- 检查基本页面加载时的 session.sessionid。
- 更多信息如何在 basepage 中检测新会话。BasePage.Session.Link
Hope this helps regards Shaz
希望这对 Shaz 有所帮助
public class BasePage : Page
{
protected string mySessionId;
private CurrentUser _currentUser;
public CurrentUser _CurrentUser
{
get { return ((CurrentUser)HttpContext.Current.Session["myCurrentUser"]); }
set { _currentUser = value; }
}
protected override void OnLoad(EventArgs e)
{
if (Session["myCurrentUser"] != null)
{
if (_CurrentUser.ProUser)
{
mySessionId = Session.SessionID; // it means New Session
}
if (!mySessionId.IsNullOrDefault() && mySessionId != Session.SessionID)
{
Session.Abandon(); //Abandon current session and start new one
}
}
}
}
回答by Praveenkumar.s
Yes.First of all Browser automatically clear session when browser is closed. you can try to capture browser close or tab close event in browser using javascript function like on before unload and on unload. Mostly onbefore unload event captures browser close event in chrome, Firefox, IE 11.
是的。首先,当浏览器关闭时,浏览器会自动清除会话。您可以尝试使用 javascript 功能在浏览器中捕获浏览器关闭或选项卡关闭事件,例如卸载前和卸载时。在 chrome、Firefox、IE 11 中,onbefore unload 事件主要捕获浏览器关闭事件。

