会话开始和动作过滤器在asp.net Mvc4中处理会话超时?
创建SessionExpireFilter类以继承ActionFilterAttribute
我正在尝试检测会话何时为空,然后在操作过滤器概念
中完成此操作后将用户重定向到主页Below code is follow
public class SessionExpireFilterAttribute : 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 it says it is a new session, but an existing cookie exists, then it must
// have timed out
string sessionCookie = ctx.Request.Headers["Cookie"];
if ((null != sessionCookie) && (sessionCookie.IndexOf("ASP.NET_SessionId") >= 0))
{
ctx.Response.Redirect("~/Account/JIOLogin");
}
}
}
base.OnActionExecuting(filterContext);
}
}
**To add a with in web.config some changes**
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime maxRequestLength="104857600" />
<customErrors mode="Off" />
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="1" />
</authentication>
**<sessionState timeout="20"></sessionState>**
<pages controlRenderingCompatibilityVersion="4.0" />
</system.web>
Controller level to apply Attribute is [SessionExpireFilter]
[SessionExpireFilter]
public class HomeController : Controller
{
dbEntities1 db = new dbEntities1();
public ActionResult Index()
{
return View();
}
}