Friends, many times we get requirements to increase the session timeout expiry time on the application server, so what we do basically we go in web.config file and set session time out to some time period, by default its 20 minutes, but remember sometime you increase time period in web.config file and it doesn't work. Why? The reason is you also have to check your application pool recycling time period it should be more than the session expiry period which you are doing in web.config file.

Yo! But anyways you did not avoid session expiration in your application, session will still expire in your application. Let's say if user filling out a long form in your application or kept a page open and gone out for coffee. So now what will happen? Session will expire right by the time use will be back and resume with the work.

Some people do session resetting in code behind by checking if session is null then read User ID () from cookies and reset it. Or if it is intranet application and AD authentication then you get the current logged in user in system and set it in session. Now if you don't wanna do all above crap in your application, what all you wanna do is don't let session to get expired unless user closes the browser.

To implement this behavior in your applications simply follow below steps:

  1. First of all here is some brief info about working session state in ASP.NET. By default it is set to 20 minutes, so if user sends a request to the server (in simple language, browse a page) then server stores the time when request came and extends the session time out to 20 minutes.If after 10 minutes user sends again a request it keeps extended to 20 minutes. If user doesn't send any request to server til 20 minutes then session get expired. This is basic working of session state.
  2. From first step you know that server need to get any request from client to keep session alive. So now we can think of some methodology to keep pinging the server. Below is the script which will keep sending request to the server and will keep session alive.

    Create a SessionCheck.aspx page in your application. You can keep this page blank so that if it gets called from the browser then it should not be over loaded.

    Using Java Script:
    Image will be used to keep session alive by changing image src, assigning this property to some.
    <img id="imgSessionCheck" width="1" height="1" />

    <script type="text/javascript" >
    //Variable used to prevent caching on some browsers and knowing the count how many times user sends request to server.
        var counter;
        counter = 0;

        function KeepSession() {
    // Increase counter value

        // Gets reference of image
        var img = document.getElementById("imgSessionCheck");

        // Set new src value, which will send a request to the server
    img.src = "http://servername:port/appName/SessionCheck.aspx?count=" + counter;

        // now schedule this process to happen in some time interval, in this example its 1 min
        setTimeout(KeepSession, 60000);

        // Call this function for a first time

    Using JQuery:
    <script language="javascript" type="text/javascript" src=""></script>
    <script language="javascript" type="text/javascript">

        function KeepSession() {
        // A request to server

        //now schedule this process to happen in some time interval, in this example its 1 min
        setInterval(KeepSession, 60000);

        // First time call of function

So it's simple. You just need to put this script in your page.