MVC 6 - 注销后防止缓存

时间:2016-02-21 14:22:50

标签: caching model-view-controller asp.net-identity

我正在使用MVC 6,我已经实现了Identity 3.0进行身份验证。

我试图阻止用户在注销后点击浏览器后退按钮。我遇到的最接近的工作解决方案似乎不适用于MVC 6。

有人可以帮忙吗?

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public sealed class NoCacheAttribute : ActionFilterAttribute
{
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
    filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
    filterContext.HttpContext.Response.Cache.SetValidUntilExpires(false);
    filterContext.HttpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
    filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
    filterContext.HttpContext.Response.Cache.SetNoStore();

    base.OnResultExecuting(filterContext);
}
}

1 个答案:

答案 0 :(得分:0)

你可以使用它。

    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
    public sealed class NoCacheAttribute : ActionFilterAttribute
    {
        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            filterContext.HttpContext.Response.Headers.Add("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
            filterContext.HttpContext.Response.Headers.Add("Pragma", "no-cache"); // HTTP 1.0.
            filterContext.HttpContext.Response.Headers.Add("Expires", "-1"); // Proxies.

            base.OnResultExecuting(filterContext);
        }
    }