操作筛选器正在启动,但重定向不在';没有发生
本文关键字:重定向 筛选 启动 操作 | 更新日期: 2023-09-27 18:20:35
我发现会话到期时要捕获的代码:
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.Misclasses{公共类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正在调用预付款,似乎在退货后重新加载。
使用此:
filterContext.Result = new RedirectResult("~/Home/Login");
return;
代替:
ctx.Response.Redirect("~/Home/Login");