我發現這個代碼,當我的會話到期趕上:行爲過濾器被解僱,但重定向是不會發生
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace SuburbanCustPortal.MiscClasses
{
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("~/Home/Login");
}
}
}
base.OnActionExecuting (filterContext);
}
}
}
它發射沒有問題,但被調用動作仍在運行它的過程:
[Authorize]
[SessionExpireFilter]
public ActionResult PrePayment(PaymentModel.PrePayment model)
{
if (string.IsNullOrWhiteSpace(Session[SessionEnums.CurrentAccountGuid.ToString()].ToString()))
{
return RedirectToAction("AddCustomer", "Customer");
}
if (TempData["ViewData"] != null)
{
ViewData = (ViewDataDictionary) TempData["ViewData"];
}
...
[SessionExpireFilter]
是在此代碼運行,但然後將它與導致問題,因爲會話不再有效方法的其餘部分繼續進行。第一行PrePayment
出現錯誤,因爲它正在返回。
不應該重定向阻止這種情況發生?
編輯**
我所做的修改建議,現在有:
命名空間SuburbanCustPortal.MiscClasses { 公共類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/LogOn/");
filterContext.Result = new RedirectResult("~/Account/LogOn");
return;
}
}
}
base.OnActionExecuting (filterContext);
}
}
}
預付款被AccountScreen調用,看起來它正在重新加載後返回。
在每一個代碼路徑,base.OnActionExecuting被調用。設置響應後,您是否嘗試過立即返回? –
而不是運行'base.OnActionExecuting'? – ErocM
是的。編輯:請參閱@YairNevet的答案。 –