会话在浏览器刷新按钮上过期

时间:2013-06-19 12:44:07

标签: c# javascript jquery asp.net

您是否知道如何仅在asp.net中使“浏览器刷新时的会话”按钮到期。谢谢。您也可以在C#或Js或Jquery中提供答案......

2 个答案:

答案 0 :(得分:1)

在c#中使用:

Session.Abandon();

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

检查网页是否已刷新的逻辑

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

第一次访问页面时存储cookie。刷新时,检查您的cookie是否存在。

请参阅:Check if page gets reloaded or refreshed in Javascript

基于此,您可以在刷新页面时应用清除会话。

以下是供参考的代码:

$(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会自动清除。

答案 1 :(得分:1)

//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();
}