How to prevent AJAX polling keeping Asp.Net sessions alive How to prevent AJAX polling keeping Asp.Net sessions alive ajax ajax

How to prevent AJAX polling keeping Asp.Net sessions alive


As this question is old I am assuming it has been resolved or a workaround implemented. However, I wanted to mention that instead of AJAX polling the server to perform an operation we have utilized SignalR which allows both the client to communicate with the server via JQuery and/or the server to notify the client.

Check it out: Learn About ASP.NET SignalR


add below code to your controller action that you are reference for polling.Convert this into an attribute so it can be used everywhere. This line will not extend session timeout

    [HttpPost]    public ActionResult Run()    {        Response.Cookies.Remove(FormsAuthentication.FormsCookieName);        return Json("");    }


I have done this by creating a hidden page in an i-Frame. Then using JavaScript it posts back every 18 minutes to keep the session alive. This works really well.

This example is from a ASP.NET Forms project but could be tweaked for MVC.

  1. Create a page called KeepSessionAlive page and add a meta refresh tag

    meta id="MetaRefresh" http-equiv="refresh" content="21600;url=KeepSessionAlive.aspx"

  2. In the code behind

    protected string WindowStatusText = "";
    protected void Page_Load(object sender, EventArgs e){    //string RefreshValue = Convert.ToString((Session.Timeout * 60) - 60);    string RefreshValue = Convert.ToString((Session.Timeout * 60) - 90);    // Refresh this page 60 seconds before session timeout, effectively resetting the session timeout counter.    MetaRefresh.Attributes["content"] = RefreshValue + ";url=KeepSessionAlive.aspx?q=" + DateTime.Now.Ticks;    WindowStatusText = "Last refresh " + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString();}
  3. Add the hidden iFrame in a master page

    iframe ID="KeepAliveFrame" src="KeepSessionAlive.aspx" frameBorder="0" width="0" height="0"

Download example