Create SessionExpireFilterAttribute class in APP_START folder
public class SessionExpireFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
HttpContext ctx = HttpContext.Current;
if (ctx.Session != null)
{
if (ctx.Session.IsNewSession)
{
string sessionCookie = ctx.Request.Headers["Cookie"];
if ((null != sessionCookie) && (sessionCookie.IndexOf("ASP.NET_SessionId") >= 0))
{
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary
{
{ "action", "CreateLogin" },
{ "controller", "LoginUser" },
{ "returnUrl", filterContext.HttpContext.Request.RawUrl}
});
return;
}
}
}
base.OnActionExecuting(filterContext);
}
}
Required Namespaces:-
using System.Web.Mvc;
using System.Reflection;
using System.Web.Security;
using System.Web.Routing;
In Controller we need to add in the below format
[SessionExpireFilter]
public ActionResult GetData()
{
return view();
}
In web.config we have to add session timeout in the following manner
<sessionState mode="InProc" timeout="20">
</sessionState>
public class SessionExpireFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
HttpContext ctx = HttpContext.Current;
if (ctx.Session != null)
{
if (ctx.Session.IsNewSession)
{
string sessionCookie = ctx.Request.Headers["Cookie"];
if ((null != sessionCookie) && (sessionCookie.IndexOf("ASP.NET_SessionId") >= 0))
{
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary
{
{ "action", "CreateLogin" },
{ "controller", "LoginUser" },
{ "returnUrl", filterContext.HttpContext.Request.RawUrl}
});
return;
}
}
}
base.OnActionExecuting(filterContext);
}
}
Required Namespaces:-
using System.Web.Mvc;
using System.Reflection;
using System.Web.Security;
using System.Web.Routing;
In Controller we need to add in the below format
[SessionExpireFilter]
public ActionResult GetData()
{
return view();
}
In web.config we have to add session timeout in the following manner
<sessionState mode="InProc" timeout="20">
</sessionState>
No comments:
Post a Comment