“会话在浏览器刷新时过期”按钮

Session Expire on Browser Refresh button

本文关键字:过期 按钮 刷新 会话 浏览器      更新时间:2023-09-26

你知道如何在asp.net中过期浏览器刷新按钮的会话吗?谢谢。你也可以用c#或Js或Jquery提供你的响应…

//this will solve the postback part (button click). 
//The refresh you should handle with querystring params
protected void Page_Load(object sender, EventArgs e) 
{
  if(Page.IsPostBack)
     Session.Abandon();
}

Session.Abandon();

这将清除所有会话并分配新的会话密钥,并且也将触发Session_OnEnd()事件。

检查页面是否刷新的逻辑

如果您想检查页面是否刷新,那么您可以使用cookie。

第一次访问时保存一个cookie。刷新时,检查cookie是否存在

参见:检查页面是否在Javascript中被重新加载或刷新

基于此,您可以在页面刷新时应用clear session。

下面是供参考的代码:

$(document).ready(function() {
  if(document.cookie.indexOf('mycookie')==-1) {
    // this mean cookie doesn't exist and user visited first time
    document.cookie = 'mycookie=1';//set the cookie to check for next time
  }
  else {
    // cookie is not null i.e. page is refreshed, 
    //So, make an ajax call to handler and use Session.Abandon() on handler in c# code.
  }
});

另外,当浏览器关闭时,cookie会自动清除