The best approach for handling session timeouts.
I say that there are 2 main cases.
One of them is when users enter little or no data, and just read reports, or think little with the mouse. In this case, there is no easy way to tell him that the session will expire. If you check the time remaining before the session calling the code behind, then automatically renew the session. Then, if you have a timer to count the session, then maybe the user has a new tab in your website open and the session expires, but not the time you noticed using javascript and the user received the wrong message.
So, for me, when a user enters little or no data, just let the session expire, if he loses one click, he will do it later.
Secondly, when the user needs to enter a lot of data , it may take some time, for example, a long text to write and fix it. In this case, I use the method below, and I do not allow the session to exit.
How to keep a session open as long as the browser.
Here is a very nice and simple method, I am using an image that I am reloading before the session is timed out using JavaScript.
<img id="keepAliveIMG" width="1" height="1" src="/img/ui/spacer.gif?" /> <script language="javascript" type="text/javascript"> var myImg = document.getElementById("keepAliveIMG"); if (myImg){ window.setInterval(function(){ myImg.src = myImg.src.replace(/\?.*$/, '?' + Math.random()); }, 6000); } </script>
In the third case, you can do this. We take care that the session ends only after publication. When the user enters some data, and in the message back, the application redirects him to the login page and the message is lost.
In this third case, you can record the message data and save it until the user re-logs in. You write message data to global.asax at
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
This is a function that is called before being redirected to the login page, and there you can see if you have data for publication and use necessary for entering the system, you save this data, broadcast to a new redirect page, broadcast to the server (possibly in a session, possibly on your temporary database).
Now, after logging in again, you redirect it again to the last page with saved messages, and the user continues to work as is.
The only trick here is to make a middle page that displays the form with the latest published data and automatically redirects the javascript call.