Here’s an interesting problem I encountered recently. With a traditional ASP.NET web application each time a user requests a page from the server, the server will reset the user’s session timeout counter and hence prevent the user’s session from timing out. However, suppose I have an AJAX.NET application which is using AJAX calls and client-side Javascript to periodically update part of a page; for example I’m periodically updating a list of meeting events for the user. Do the AJAX calls keep my session alive?
The answer is No. From an ASP.NET session point of view these AJAX calls are not keeping the ASP.NET session alive and therefore after 20 minutes (or what ever you have the session timeout value set to) the user’s session will timeout. This in itself is not good if you want the user’s session to be maintained. If additionally your AJAX.NET calls are relying on Session data (for example a UserID) to return the correct data then this is downright bad!
How to implement a Session keep-alive timer for your ASP.NET AJAX application
Here’s a solution below. Basically you need to:
1. Create a new web form - SessionKeepAlive.aspx for example. Leave the HTML as is. In the code behind, add the following in the Page_Load method:
protected void Page_Load(object sender, EventArgs e)
{
// No need to put any code here but you can return some value (eg. "alive")
// as shown below and check this is received on the client.
Response.ContentType = "text/html";
Response.Write("alive");
}
2. In your main page which requires the session keep-alive timer, place the following Javascript:
<script type="text/javascript">
<!--
function sessionKeepAlive() {
var wRequest = new Sys.Net.WebRequest();
wRequest.set_url("SessionKeepAlive.aspx");
wRequest.set_httpVerb("POST");
wRequest.add_completed(sessionKeepAlive_Callback);
wRequest.set_body();
wRequest.get_headers()["Content-Length"] = 0;
wRequest.invoke();
}
// This callback function processes the
// request return values. It is called asynchronously
// by the current executor.
function sessionKeepAlive_Callback(executor, eventArgs)
{
// No need to do anything, but if you are sending a value
// from the server as an additional safety measure, then
// you can check that here.
}
// Set 12 minute .NET session keep alive timer...
window.setInterval( isessionKeepAlive(), 12 * 60 * 1000);
// -->
</script>
The above code basically sets up a timer to fire the sessionKeepAlive() function every 12 minutes. The sessionKeepAlive() function basically makes an call to the SessionKeepAlive.aspx page you created which in effect will reset the user’s session timeout counter!
Related Stuff
-
MooV: Using cutting edge Video phones and Software Video Phones - coupling all that with VoIP and empowering the disabled.
-
Moo Telecom: VoIP communications made easy - Ring anyway with the fun and ease of using a normal phone
-
TagR:Mobile Social Network with Real Time Locations Based services, and Ambience Intelligence, VoiP, IM, Skype, Googletalk, Mapping, Flickr, Events, Calendaring, Scheduling, SecondLife Support
-
ClearSMS : ClearSMS is a Web-based application that lets you send bulk SMS messages to your customers, contacts, or just about anyone.
-
Jajah:jah is a VoIP (Voice over IP) provider, founded by Austrians Roman Scharf and Daniel Mattes in 2005[1]. The Jajah headquarters are located in Mountain View, CA, USA, and Luxembourg. Jajah maintains a development centre in Israel.
-
Skype: It’s free to download and free to call other people on Skype. Skype the number one voice over ip software
- PrivatePhone: a free local phone number with voicemail and messages you can check online or from any phone.

Original Source: