关闭浏览器选项卡时清除cookie [英] clearing cookies on closing browser tab
问题描述
我面临的问题如下:
登录后,用户通过关闭浏览器选项卡关闭网站而不是注销。
会话被维护,我试图再次登录即时收到错误。
i想要清除cookie中保持的会话值直接关闭标签。
无论如何都可以这样做
I m facing problem as follows:
after login when user closes the website by closing the browser tab and not by logging out.
the session is maintained and i m trying to log in again i m getting the error.
i want to clear the session value maintained in cookies after closing the tab directly .
is der any way to do so
推荐答案
可以使用JQuery完成。看看这里
It can be done using JQuery. Take a look here
您可以使用javascript删除cookie。如果你将它附加到浏览器关闭事件,它将工作。
You can delete cookies using javascript. And if you attach this to the browser close event, it will work.
<body onunload="deleteAllCookies()">
function deleteAllCookies() {
var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i];
var eqPos = cookie.indexOf("=");
var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
}
}
这将清除所有cookie onUnload,但cookie应该使用http设置。 (阅读有关xss和保护cookie的信息:保护您的Cookie:HttpOnly [ ^ ])
如果您的cookie受到良好保护,则无法使用javascript删除它们。
解决方案是创建一个调用aspx页面的javascript函数,清除会话并删除所有cookie。
This will clear all cookies onUnload, but cookies should be set using http-only. (read about xss and securing cookies : Protecting Your Cookies: HttpOnly[^])
And if your cookies are well protected, they can't be deleted using javascript.
A solution is to create a javascript function which call an aspx-page which clear the session and remove all cookies.
window.onbeforeunload = function(){
。 get (< span class =code-string>' removeSession.aspx',function(){
// 一切都很好
});
}
.get('removeSession.aspx', function() { // all good }); }
在你的removeSession.aspx你可以在page_load中做你的东西
In your removeSession.aspx you can do your stuff in page_load
protected void Page_Load(object sender, EventArgs e)
{
Session.Abandon();
foreach (string cookie in Request.Cookies)
{
Response.Cookies[cookie].Expires = DateTime.Now.AddDays(-1);
}
}
这篇关于关闭浏览器选项卡时清除cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!