-1

how can i make work the code blocks when session expired on ASP.NET c# ? i want to do something like

if(session==expired)
{
//do something
}

or just want to ask. if (browser is closed) do something; thank you a lot

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • What is the thing you plan to do? i.e. what is `// do something`. – mjwills Dec 12 '18 at 07:31
  • like in SQL command insert into ...something.. its a do something. it method will do something whatever i want. what type of a question is it ? – Metin Birdal Dec 12 '18 at 11:55
  • When you say `session`, tell us what you mean by that. Do you mean ASP.NET's notion of `Session`? Or do you mean when the user closed their browser? Or do you mean when they clicked `Logout` in your website manually? Or do you mean when they were automatically logged out since the forms authentication timeout expired? Or something else? – mjwills Dec 12 '18 at 11:57
  • oh ok sorry. i mean when user closes browser or electricity is gone.when 20 mins of session are done , stop the users work. like do something. – Metin Birdal Dec 12 '18 at 12:48
  • Check this link. Might offer some insight. https://stackoverflow.com/questions/10480110/how-to-check-whether-session-is-expired-or-not-in-asp-net – Nikos Dec 12 '18 at 13:02
  • @NikosV The accepted answer there is not to do with ASP.NET's Session, but instead whether the request has an identity associated with it (i.e. are they logged in). The two concepts are often conflated, but they aren't the same thing. – mjwills Dec 12 '18 at 13:11
  • ok guys thanks for answers , i think there is nothing to do with it. cause its programmatically impossible what i asking for.i think like, think just simple,like facebook. facebook has some user sessions and when user logout you can see is user online or offline.i wanted to do like if user getting offline , insert something on sql table.but user gets offline not with logout button, but with electricity gone or browser is closed. so i think this is impossible to do.cause when computer shutdown there will no data and program cant update itself.it maybe possible to do with hosting side computer – Metin Birdal Dec 13 '18 at 09:20
  • but im not sure if it can be. – Metin Birdal Dec 13 '18 at 09:20
  • @MetinBirdal Please show us your web.config where you are defining session timeout and forms auth timeout. – mjwills Dec 13 '18 at 11:54
  • i havent define session or forms auth cause i dont work with forms auth , only sessionID's and these session ID's have their standart expiration time 20 mins. thats all. – Metin Birdal Dec 13 '18 at 12:29

1 Answers1

0

In Internet explorer, there is an onUnload event of the body. Mozilla event here: https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload

However, the event only detects the ‘X’ on the browser. If user terminates the process, the event isnt called.

Try the following code:

Client side

client side script:

 <body onbeforeunload="window.open('http://www.yourwebsite.com/backend.aspx','mywindow','width=10,height=10');">

In backend:

Code behind Page_Load

Session.Clear();
Session.Abandon();
    Response.Redirect("Login.aspx");
}

Reference: C# Clear Session

Gauravsa
  • 6,330
  • 2
  • 21
  • 30