我有一个授权过滤器类:
public class ValidateSessionTokenFilter : IAuthorizationFilter
{
public void OnAuthorization(AuthorizationContext filterContext)
{
if (filterContext.HttpContext.Request.IsAjaxRequest())
{
filterContext.Result = new JsonResult
{
Data = new { Message = "Session timeout" },
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
filterContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
}
}
}
过滤器有效,但问题是即使我强制返回类型为JsonResult
结果,框架总是返回重定向到登录页面。
有没有办法防止这种行为?