Session Management is an important aspect to be taken care of while writing code. Several factors play a role in deciding your strategy and it is never one size fits all. It is important to understand the context and specific requirements of a business to determine the appropriate session duration.
In this context, the application was used as an internal application to gather data on all the work being completed by the construction crew. This was reviewed by the managers and supervisors and thereafter billers generated the invoices.
It was important here to allow for long session durations. The users wanted the ability to remain logged in even when the application might be idle for a long time, and with no data loss. This would not be recommended in the context of personal banking, where security and avoidance of session hacking are of far greater importance.
This article explains how you can maintain a session in ASP.Net and avoid getting logged out, even when the application is idle for a long time. This method will also help in preventing data loss.
We used Handler to maintain the session in ASP.Net.
Why Handler?
- Handler makes a simple post-back call to the server to keep the session alive.
- In Handler, a session variable is accessed only if it is inherited by the interface IRequiresSessionState. The handler receives the request from the aspx page and, in turn, sends a response.
Implementation of Handler in our application using Asp.net and jQuery:
We employ Handler in our application for user data maintenance and to prevent session time out.
How Handler Works:
- Create a handler by right-clicking on the website and adding an item by selecting a generic handler. Eg:(KeepActive.ashx) - extension of handler is ASHX.
- Make a call to the handler using jQuery function.
Code snippet:
<%@ WebHandler Language="C#" Class="KeepActive" %>
using System;
using System.Web;
using System.Web.SessionState;
public class KeepActive : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
try
{
context.Session["Active"] = DateTime.Now;
}
catch { }
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
- After the handler is created, create a jQuery function named “setinterval”, which runs at an interval to update session.
- Please find the below jQuery code, which is called on load of the page and it has a function named “setinterval”, which get triggered after every five minutes:
Code snippet:
window.onload = function () {
var idleInterval = setInterval(timerIncrement, 300000); // for 5 minutes
}
- The above function calls another function named “timerIncrement”, which triggers and calls handler named “KeepActive” after every five minutes. It is necessary to update the session and keep it alive
Code snippet:
function timerIncrement() {
idleTime = idleTime + 1;
if (idleTime > 1) { // Greater than 10 minutes
if (typeof GetHiddenForemanReportID == 'function') {
var foremanReportID = GetHiddenForemanReportID();
jq.post("/KeepActive.ashx?foremanReportID=" + foremanReportID, null, function () {
});
MetaSys Software’s developers have successfully delivered applications using ASP.Net Core and .Net & ASP.Net Framework. For more details, see https://www.metasyssoftware.com/dot-net. Contact us with your project requirements now!