Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;

           // check if session is supported
           if (ctx.Session != null)
           {
               // check if a new session id was generated
               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;
                           //viewData.Model = username;
                           filterContext.HttpContext.Response.StatusCode = 504;
                           //filterContext.Result = new ViewResult { ViewName = "/Home/SessionExpire.aspx", ViewData = viewData };

                           //ctx.Response.Redirect("~/Home/Login");
                           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 the browser session has expired...
                    if (ctx.Session["UserName"] == null)
                    {
                        if (filterContext.HttpContext.Request.IsAjaxRequest())
                        {
                            //  filterContext.HttpContext.Response.StatusCode = 504;
                            // filterContext.Result = new JsonResult { Data = "_Logon_" };
                        //    filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary {
                        //{ "Controller", "Home" },
                        //{ "Action", "SessionExpire" }
                        //    });
                            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
Posted
Comments
fjdiewornncalwe 4-Oct-12 15:59pm    
Please remove your email address from your username.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900