这是解决方案:
using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
namespace Capron.MVC.Filters
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
public class SessionExpireFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext.ActionDescriptor.ControllerDescriptor.ControllerName.ToLower() == "home"
&& (filterContext.ActionDescriptor.ActionName.ToLower() == "index"
|| filterContext.ActionDescriptor.ActionName.ToLower() == "ındex")) {
base.OnActionExecuting(filterContext);
return;
}
HttpContext ctx = HttpContext.Current;
if (ctx.Session != null)
{
if (ctx.Session.IsNewSession)
{
if (filterContext.HttpContext.Request.IsAjaxRequest())
{
string sessionCookie = ctx.Request.Headers["Cookie"];
if (sessionCookie != null && sessionCookie.IndexOf("ASP.NET_SessionId") >= 0)
{
filterContext.HttpContext.Response.StatusCode = 401;
filterContext.HttpContext.Response.End();
}
}
else
{
ctx.Response.Redirect("~/Home/Index");
}
}
}
base.OnActionExecuting(filterContext);
}
}
}
并将此属性添加到我的控制器:
[SessionExpireFilterAttribute]
public class HomeController : BaseController
{
...