Detect session expiration

In my .net application I need to store the page that was displayed before session expiration, for example if the user was in the page A and the session expire when hi will acces to the app I have to redirect him to the page A.

My idea is to detect session expiration and if the event happen save the data in data base.

Answers


You can use simply a session variable. On page load check if the variable exists, and if it doesn't exist anymore you can save the data you need.

You can retrieve the URL also if the session has expired. UserId should be stored in some "permanent" place, like a hidden field in the page, so you can retrieve it also if the session has expired.


To answer your question: You can use the Session_End method in global.asax to detect an expiring session.

Unfortunately, at that time, the information about the page that the user accessed last is no longer (conveniently) available, so I doubt that this will help you solve your underlying problem.

As an alternative, I would suggest that you update the user's "last visited page" in your database every time a page is opened. You can do that globally in your master page, if you have one.


You can do the following:

1) For each request (Application_BeginRequest) store in the session context data (user, URL etc.):

Session["LastPageData"] = new { Url = ..., };

2) Before session is about to end, you can use the information to store in a database, for example:

void Session_End(Object sender, EventArgs E) {
    // do persistence logic here based on Session["LastPageData"]
}

If some information is not available in Session_End, you can persist the information on each request, but this will mean extra overhead (INSERT/UPDATE for each request)


As you will be already knowing being a web application, the interaction between your user agent and server is disconnected i.e. web server or browser does not have information about the current state of user, so there are no event which will let you know that session has expired, though you can compute it based on session time out values and check whether the session has expired or not (or do any similar thing).

So, you can know that session has expired only when the request reached your server and the request is processed.

For solution to your problem, on session expiry you will be redirecting the user to login page, before redirection you can put the current page url relative path in the URL as a parameter and on successful login, you can redirect user to previous page by reading it from query string.

Your login url will look like this https://myapp.com/Account/Login?returnUrl=%2FMyAccount

Here MyAccount is the last visited URL where session has expired.

The same approach is used by asp.net MVC authorization, you may have a look at it using the default template.


you may use Session_OnEnd, Session_End() even in your global.asax file,

In this event you may save your session value into the db explicitly because Session_OnEnd and Session_End don't get called if session is out proc (Saving session in state server Or sql server).

Now when user will open the application again then on each page load you have to check the value of session variable (I'm not sure if it can be done itself into Session_Start or Apllication_Start events in global.asax) and if it is null then get the latest value from the db and redirect to respective page.


I resolve the problem with javascript, the approach is to have variable that represents session timeout and in make global javascript method that declenche session Save before expiration. $(document).ready(function () { var saveSessionTimer; clearTimeout(saveSessionTimer); //timerToSaveSession = sessionExpireTime - 10 seconds saveSessionTimer = setTimeout(saveSession, timerToSaveSession); }); where saveSession is the method that call controller and make the save.


Need Your Help

Papaparse proper encoding for strange characters

javascript papaparse

I am trying to read strange characters from a .csv file using Papaparse.