-3

Do you have any idea on how to expire the Session on Browser Refresh button only in asp.net. Thanks. You can also provide your responses in C# or Js or Jquery...

Dylan
  • 137
  • 1
  • 2
  • 10
  • 1
    Welcome to Stackoverflow. This is not a [real question](http://meta.stackexchange.com/questions/145677/what-is-a-real-question) for here. Did you try anything so far? Show your effort first so people might show their. Please read [FAQ] and [ask] – Soner Gönül Jun 19 '13 at 12:47
  • browser refresh button repeats the previous request (assuming there was no ajax request in between) per my knowledge i don't know of a way to flag the refresh with any other attributes to create a special request. – DevZer0 Jun 19 '13 at 12:47
  • I really don't have an idea. So I didn't place the code. – Dylan Jun 19 '13 at 12:49
  • Why would you want to do this? It sounds like there is something else incorrectly structured in your code. – Paddy Jun 19 '13 at 12:50
  • Why do you want to do that? It's not a common behaviour for a web application. – Alexander Jun 19 '13 at 13:14

2 Answers2

1

in c# use:

Session.Abandon();

This will clear all session and assign new session key and Session_OnEnd() event will also be fired.

Logic to check if page is refreshed

If you want to check the page is refreshed or not, then you can use cookies for that matter.

Store a cookie when page is visited first time. On refresh, check if your cookie exists or not.

see: Check if page gets reloaded or refreshed in Javascript

based on this you can apply clear session when your page is refreshed.

Here is code for reference:

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

Also, when browser will be closed, cookie will be automatically cleared.

Community
  • 1
  • 1
Sanjeev Rai
  • 6,721
  • 4
  • 23
  • 33
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();
}
Thiago Custodio
  • 17,332
  • 6
  • 45
  • 90