Step 1 - First came up a extension method which will check if server session expires and the code is as follows:
public static bool IsSessionExpired(this HttpSessionStateBase session)
{
if (session == null || !session.IsNewSession)
{
return false;
}
// If it says it is a new session, but an existing cookie exists, then it must have timed out
string sessionCookie = HttpContext.Current.Request.Headers["Cookie"];
return sessionCookie != null && sessionCookie.Contains("ASP.NET_SessionId");
}
Step 2 - Design an Attribute that will call the above routine on every action executed in any controller of the Application. You can apply this attribute on your BaseController at the class level. The code is as follows:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
public class SessionExpireAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
HttpContext ctx = HttpContext.Current;
// check if session is supported
if (ctx.Session != null)
{
if (filterContext.HttpContext.Session.IsSessionExpired())
{
var viewData = filterContext.Controller.ViewData;
viewData.Model = username;
filterContext.HttpContext.Response.StatusCode = 504;
filterContext.Result = new ViewResult { ViewName = "SessionTimeout", ViewData = viewData };
}
}
}
}
Step 3 - Equip your Ajax Calls with Error Handler that will check for Error Status Code = 504 and redirect the user to SessionTimeout page.
//$.Ajax Get/Post Calls
$("#NewTemplate").live("click", function () {
if (confirmNavigation($(this))) {
$("#selectedTemplateId").val(0);
$.ajax({
type: "GET",
url: Urls.NewTemplate,
async: false,
cache: false,
success: function (cb) {
if (!cb) {
alert("Unable to load the screen");
}
else {
uiChanges = true;
$("#divTemplate").html(cb);
}
},
error: function (xhr, textStatus, errorThrown) {
redirectOnSessionTimeout(xhr.status);
}
});
}
});
//$.load Ajax Calls
function loadAuditTableGrid(name) {
$("#auditTableGrid").load(
Urls.AuditTable,
{ auditTableName: name },
function (response, status, xhr) { redirectOnSessionTimeout(xhr.status); });
}
//$.getJSON way
reqJSON(Urls.CheckForActiveState, { id: selectedId},
function (result) {
if (!result.success)
$("#Message").show();
else
$("#Message").hide();
}, function (xhr, textStatus, errorThrown) {
redirectOnSessionTimeout(xhr.status);
});
function reqJSON(url, params, success, error) {
var CallParams = {};
CallParams.type = params.Method || "POST";
CallParams.url = url;
CallParams.processData = true;
CallParams.data = params;
CallParams.dataType = "json";
CallParams.success = success;
if (error) {
CallParams.error = error;
}
$.ajax(CallParams);
}
function redirectOnSessionTimeout(status) {
if (status == "504") {
location.href = Urls.SessionTimeout;
}
}
hi!,I really like your writing very so much!
ReplyDeleteMalwarebytes
Office 2017
Corel
Corel
Thanks for sharing useful information. I am giving best MVC online training
ReplyDeleteMVC online training
online MVC training
asp.net with MVC training
MVC training
Really Good blog post.provided a helpful information.I hope that you will post more updates like this.
ReplyDeleteAWS Training in HRBR Layout
AWS Training in Kalyan Nagar
Best AWS Training Institute in kalyan Nagar Bangalore
I read this post two times, I like it so much, please try to keep posting & Let me introduce other material that may be good for our community.
ReplyDeleteSelenium training in Chennai
Selenium training in Bangalore
Selenium training in Pune
Selenium Online training
ReplyDeleteThis is quite educational arrange. It has famous breeding about what I rarity to vouch. Colossal proverb.
This trumpet is a famous tone to nab to troths. Congratulations on a career well achieved. This arrange is synchronous s informative impolites festivity to pity. I appreciated what you ok extremely here
Selenium training in bangalore
Selenium training in Chennai
Selenium training in Bangalore
Selenium training in Pune
Selenium Online training
Nice Post! Thank you for sharing very good post, it was so Nice to read and useful to improve my knowledge as updated one, keep blogging.
ReplyDeleteASP.Net Training in Electronic City
I feel really happy to see your blog and look forward to many more entertaining times reading here. Thanks once more for all the details.
ReplyDeletejava training in chennai
java training in tambaram
aws training in chennai
aws training in tambaram
python training in chennai
python training in tambaram
selenium training in chennai
selenium training in tambaram
Its such as you learn my mind! You appeаr tо grasp ѕo much approximately this, such as you wrote the book in it or something.
ReplyDeleteI think that you could ɗo wіth some percent to pressure the mesѕage home a little bit,
but instead of that, this iѕ excellent blog. An excellent
read. I ԝilⅼ defіnitely be back.
angular js training in chennai
angular js training in velachery
full stack training in chennai
full stack training in velachery
php training in chennai
php training in velachery
photoshop training in chennai
photoshop training in velachery
Really Good blog post.provided a helpful information.I hope that you will post more updates like this.
ReplyDeleteoracle training in chennai
oracle training in omr
oracle dba training in chennai
oracle dba training in omr
ccna training in chennai
ccna training in omr
seo training in chennai
seo training in omr
Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
ReplyDeleteweb designing training in chennai
web designing training in annanagar
digital marketing training in chennai
digital marketing training in annanagar
rpa training in chennai
rpa training in annanagar
tally training in chennai
tally training in annanagar
Hi,Great information.Thanks for sharing.I always read your blog and get lot of useful information.
ReplyDeletedata science training in chennai
data science training in porur
android training in chennai
android training in porur
devops training in chennai
devops training in porur
artificial intelligence training in chennai
artificial intelligence training in porur