我有 HTTPS 和 HTTP 问题。看来我的过滤属性正在被缓存。当我登录时,它应该自动将任何 http 页面发送到 https,它确实这样做了。然后,当我注销时,它保留在 https 上,这很好,但如果我尝试手动转到非 http 页面,即使我已登录,它也会再次重定向到 https 页面。 RequireSecure 始终为 true,因为我的日志记录信息表明了这一点是。 FilterAttributes 是否被缓存?如果是,我该如何解决这个问题?
public class RequireHttpsAttribute : System.Web.Mvc.RequireHttpsAttribute
{
protected static readonly NLogLogger Logger = new NLogLogger();
public bool Ignore = false;
public bool RequireSecure = false;
public override void OnAuthorization(AuthorizationContext filterContext)
{
if (filterContext.RequestContext.HttpContext.User.Identity.IsAuthenticated)
{
Logger.Info("Require HTTPS User Authenticated");
RequireSecure = true;
}
if (filterContext.RequestContext.HttpContext.Request.IsLocal)
RequireSecure = false;
if (RequireSecure)
{
Logger.Info("Require HTTPS");
base.OnAuthorization(filterContext);
}
}
}
}
最佳答案
您所经历的是正确的行为。详细信息可以在这里找到:
- UnitOfWork in Action Filter seems to be caching .
- http://www.asp.net/learn/whitepapers/mvc3-release-notes#RTM-BC
简短摘录:
...In ASP.NET MVC 3, filters are cached more aggressively. Therefore, any custom action filters which improperly store instance state might be broken.
我建议存储对过滤器属性
重要的数据:请求范围
filterContext.HttpContext.Items
作为 session 范围(就像您的情况一样):
filterContext.HttpContext.Session
filterContext.HttpContext.Profile
关于asp.net-mvc - FilterAttributes 是否在 ASP.NET MVC 中缓存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13655375/