Skip to content

Instantly share code, notes, and snippets.

@MarioBinder
Created November 4, 2014 06:19
Show Gist options
  • Save MarioBinder/821a7105a04bc630b47d to your computer and use it in GitHub Desktop.
Save MarioBinder/821a7105a04bc630b47d to your computer and use it in GitHub Desktop.
SessionExpireFilterAttribute
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
public class SessionExpireFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var ctx = HttpContext.Current;
if (ctx.Session["RegisteredUser"] == null || !filterContext.HttpContext.Request.IsAuthenticated)
{
FormsAuthentication.SignOut();
ctx.Session.Clear();
if (filterContext.HttpContext.Request.IsAjaxRequest())
{
filterContext.Result = new JsonResult { Data = "_Logon_" };
}
else
{
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary {
{ "Controller", "Account" },
{ "Action", "LogOn" }
});
}
}
base.OnActionExecuting(filterContext);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment