0

当我的会话到期时,我发现此代码可以捕获:

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 正在调用预付款,并且似乎在返回后正在重新加载。

4

1 回答 1

3

用这个:

   filterContext.Result = new RedirectResult("~/Home/Login");
   return;

代替:

ctx.Response.Redirect("~/Home/Login");
于 2013-06-29T17:32:57.707 回答