1

I use asp.net to manage the session state of my site. I also use jquery and $.ajax(...) for synchronous and asynchronous requests.

Normally, in asp.net if a users session times out, it can be detected via a full or partial post-back. However, suppose a partial or full-post-back does not occur because I am using jquery ajax calls to a static c# web method. Whats the best what to know if the session timeout?

PhillyNJ
  • 3,859
  • 4
  • 38
  • 64

3 Answers3

2

i would create a javascript interval to make a ajax request from time to time, something like this:

<script>
$(document).ready(function () {
    setInterval(function (){
        $.ajax({
            url: 'WebMethodhere',
            type: 'GET',
            success: function (result) {
                if(result!='true')
                {
                    //It means that the Session expired, so do somethig
                }
            }
        })
    },3000);
});
</script>

I used 3000ms in this example, but the time it's up to you.

And your webmethod could be really simple:

if(Session["WhateverSession"]==null)
{
    return false;
}
else
{
    return true;
}
  • If I query about session from the server every xxx seconds, will it renew the session on every call or not? In simple words do AJAX calls extend / renew session time or not. – haraman Oct 21 '15 at 17:48
  • simples: yes, it does - http://stackoverflow.com/questions/10370110/does-session-timeout-reset-on-every-request – Caio César S. Leonardi Oct 21 '15 at 17:53
  • The answer is **NOT simple yes**. There are conditions (cookieless or not). You must read full thread, particularly the answer by [Pankaj](http://stackoverflow.com/a/10437034/5104101) along with its **comments** and also https://ranjitkumars.wordpress.com/2010/03/02/asp-net-ajax-prevent-session-timeout/ – haraman Oct 21 '15 at 18:16
  • didn't said it was simple, just that I was giving you an "simple" answer: Yes (even though there are some exception cases). But you should have just searched by yourself instead, because that's exactly what I did when you've asked that... – Caio César S. Leonardi Oct 21 '15 at 19:17
  • I had already read that a long ago. That comment was asked just to make the OP questioner and other readers aware of exception to **yes**. Anyways, good (re)search. – haraman Oct 22 '15 at 06:07
0

There are couple of approaches to achieve it.

Approach #1: Create a generic handler and implement the IReadOnlySessionStateinterface to access session variables from this handler. Use a ajax get request and access this generic handler which provides whether session is active or not.

Approach #2: Decorate a public method of your page with [WebMethod] (using System.Web.Services) and access this method through ajax get request which provides whether session is active or not.

But all these approaches should be used just to check session is active or expired. But if the session is active, it'll renew your session - it may not be desirable. Please check that option also.

Sudipta Kumar Maiti
  • 1,669
  • 1
  • 12
  • 18
  • You could also declare the WebMethod in a base class that your pages inherit from to prevent having to create handlers or duplicate code all over the place. – DVK Oct 21 '15 at 17:46
  • Do AJAX calls extend / renew session time or not. – haraman Oct 21 '15 at 17:53
  • The session renews on each request to server, the browser supplies the ASP.NET_SessionId (Cookie) which the server uses to access session value, that's the default behavior, no difference between ajax or simple page request or postback. – Sudipta Kumar Maiti Oct 21 '15 at 18:35
0

The easiest solution was to force a post back at certain intervals.

<meta http-equiv="refresh" content="1800">
PhillyNJ
  • 3,859
  • 4
  • 38
  • 64