Shalin Gajjar
Shalin Gajjar

Reputation: 239

how to maintain session state as long as possible with asp.net 4.0

i just want to make session as long as possible. here i present my code how i maintain session with as long as possible with asp.net 4.0 with c# using ashx file.

here is my keepalive.ashx file :

<%@ WebHandler Language="C#" Class="keepalive" %>

using System;
using System.Web;
using System.Web.SessionState;

public class keepalive : IHttpHandler, IRequiresSessionState
{

    public void ProcessRequest(HttpContext context)
    {
        if (context.User.Identity.IsAuthenticated)
        {
            // authenticated sessions
            context.Response.ContentType = "text/plain";
            context.Response.Write("Auth:" + context.Session.SessionID);
        }
        else
        {
            // guest
            context.Response.ContentType = "text/plain";
            context.Response.Write("NoAuth:" + context.Session.SessionID);
        }
    }

    public bool IsReusable {
        get {
            return false;
        }
    }
}

and here is my calling code from master page :

<script type="text/javascript">
        var interval = null;
        (function () {
            // keep me alive
            interval = setInterval(function () {
                $.get('../keepalive.ashx', function (d) {
                    $('#response').append(d + '<br/>');
                });
            }, 30000);
        })();


        // If we want to stop the interval....
        function Stop() {
            clearInterval(interval);
        }
</script>

and i change some properties from web config :

 <sessionState mode="InProc" cookieless="false" timeout="2"/>

how ever this code not satisfied my needs.. please help me...

Upvotes: 0

Views: 1744

Answers (1)

Haseeb Asif
Haseeb Asif

Reputation: 1786

You can configure the timeout value for your application through web.config (which you already did to 2 mins) as follows

<configuration>
    <system.web>
      ...
      <sessionState timeout="525600 "/>
    </system.web>

</configuration>

Or you can also set it on the server for all of the applications by setting the IIS config as given on IIS site

<location path="Default Web Site">
   <system.webServer>
      <asp>
         <session allowSessionState="true" max="1000" timeout="525600" />
      </asp>
   </system.webServer>
</location>

Upvotes: 1

Related Questions