我有一個使用Windows Authentication
的MVC4應用程序。用戶可以鍵入任意10個視圖的url來加載應用程序。沒有具體的主頁會話超時處理的會話啓動和操作過濾器
如果用戶閒置超過一分鐘,我需要重定向到會話超時視圖。我將會話超時值保存在配置文件中爲一分鐘。我創建了一個action filter
來檢查一個特定的會話值。此特定會話值在Global.asax
的Session_Start
中設置。
但是,當超時時間結束時,請求再次遇到Session_Start
並且它正在分配值。因此我的動作過濾器不會重定向到錯誤視圖。 有什麼可以解決這個問題的解決方案?
的Web.Config
<system.web>
<!--Impersonate-->
<identity impersonate="true"/>
<!--Session Mode and Timeout-->
<sessionState mode="InProc" timeout="1" />
<authentication mode="Windows">
</authentication>
<authorization>
<allow users="?" />
</authorization>
</system.web>
行爲過濾
[AttributeUsage(AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
public class SessionCheckAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(System.Web.Mvc.ActionExecutingContext filterContext)
{
string controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName.ToLower();
HttpSessionStateBase session = filterContext.HttpContext.Session;
var activeSession = session["IsActiveSession"];
if (activeSession == null)
{
//Redirect
var url = new UrlHelper(filterContext.RequestContext);
var loginUrl = url.Content("~/Error/SessionTimeout");
filterContext.HttpContext.Response.Redirect(loginUrl, true);
}
}
}
的Global.asax
protected void Session_Start(object sender, EventArgs e)
{
Session["IsActiveSession"] = DateTime.Now;
}
參考:http://stackoverflow.com/questions/199099/how-to-manage-a-redirect-request-after-a-jquery-ajax-call – Lijo
和http://stackoverflow.com/questions/ 16259230/worldwide-filter-ajax-success-handlers和[408錯誤代碼](http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html) – Lijo