【发布时间】:2013-09-05 16:53:55
【问题描述】:
- 我有一个 MVC 4 Web 应用程序。
- 部署到我的 iis7 环境。
- 它使用 Windows 身份验证和模拟。
- 公司政策将其设置为 15 分钟后会话超时。
这在 asp.net 应用程序中总是可以正常工作。但是我现在有一个 MVC4 应用程序,所以我现在没有使用 global.asax 和 session_start 来确定会话是否已超时和重定向,而是设置了一个会话过期操作过滤器。
一切都按预期工作,会话超时时重定向,直到用户让应用程序空闲。应用程序池的空闲超时为 20 分钟,我认为这是这里的问题。
如果我的用户让他的应用程序保持打开状态并前往他们经常参加的会议,并且达到空闲超时,当他们回来并尝试做某个时间并且应用程序尝试在会话超时时重定向,我可以在错误日志中查看它被困在一个循环中,并且永远不会重定向。
在事实(即空闲超时)之后尝试重定向是否为时已晚?以前在 asp.net 应用程序(非 MVC)中,我过去总是在 global.asax 的会话开始中执行此操作(即在会话超时时重定向),并且效果很好。显然,这个事件总是在应用程序空闲超时开始之前被触发。
任何帮助将不胜感激,这是我的会话处理程序代码:
public class SessionExpireAttribute : ActionFilterAttribute
{
/// <summary>
/// Called by the ASP.NET MVC framework after the action method executes.
/// </summary>
/// <param name="filterContext">The filter context.</param>
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
base.OnActionExecuted(filterContext);
}
///<summary>
///Called by the ASP.NET MVC framework before the action method executes.
///</summary>
///<param name="filterContext">The filter context.</param>
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext.HttpContext.Session != null)
{
if (filterContext.HttpContext.Session.IsNewSession)
{
var sessionStateDetails =(SessionStateSection)ConfigurationManager.GetSection("system.web/sessionState");
var sessionCookie = filterContext.HttpContext.Request.Headers["Cookie"];
if ((sessionCookie != null) && (sessionCookie.IndexOf(sessionStateDetails.CookieName) >= 0))
{
if (filterContext.HttpContext.Request.IsAjaxRequest())
{
filterContext.HttpContext.Response.Clear();
filterContext.HttpContext.Response.StatusCode = 403;
}
else
{
RouteValueDictionary redirectTargetDictionary = new RouteValueDictionary();
redirectTargetDictionary.Add("action", "SessionTimeout");
redirectTargetDictionary.Add("controller", "Home");
filterContext.Result = new RedirectToRouteResult(redirectTargetDictionary);
}
【问题讨论】:
-
即使在 MVC 中你仍然可以使用 Session_Start,那么这样做有什么问题呢?
-
是 session_start 在 MVC4 中仍然有效,并且可以正确重定向。但是从我一直在阅读的博客中,我认为使用处理程序是更好的做法?有什么想法吗 ?这就是我走这条路的原因。
标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-4