When session timeout, i have to handle two things
1. page request
2. ajax request
Page request easily redirect to Session Expired page without any fault
ActionFilterAttribute
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
HttpContext ctx = HttpContext.Current;
if (ctx.Session != null)
{
if (ctx.Session.IsNewSession)
{
if (!filterContext.HttpContext.Request.IsAjaxRequest())
{
string sessionCookie = ctx.Request.Headers["Cookie"];
if ((null != sessionCookie) && (sessionCookie.IndexOf("ASP.NET_SessionId") >= 0))
{
var viewData = filterContext.Controller.ViewData;
filterContext.HttpContext.Response.StatusCode = 504;
ctx.Response.Redirect("~/Home/SessionExpire");
}
}
}
}
base.OnActionExecuting(filterContext);
}
by overriding i can easily achieve the page request..
But my problem is i cannot redirect the same thing when Ajax request from UI (jquery).
But i did this with
AuthorizationContext
override the
HandleUnauthorizedRequest here is the code What i used...
It redirect to session page. but it redirect inside the Master Page.. why it happens like. i think because of ajax request
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
public class AjaxAuthorizeAttribute : AuthorizeAttribute
{
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
HttpContext ctx = HttpContext.Current;
if (ctx.Session != null)
{
if (ctx.Session["UserName"] == null)
{
if (filterContext.HttpContext.Request.IsAjaxRequest())
{
ctx.Response.Redirect("~/Home/SessionExpire");
}
base.HandleUnauthorizedRequest(filterContext);
}
}
}
This is my what i used...
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary {
//{ "Controller", "Home" },
//{ "Action", "SessionExpire" }
// });
These lines also give the same output, What this gives
ctx.Response.Redirect("~/Home/SessionExpire");
Could you help in this..Please give the reason and solution.. for this....
Problem
Problem is all working fine when redirecting the the session expired page show inside the master page...
but sessionExpire page is not inherit the master page,
but it works fine in page request.
Thank you in Advance