仅为未经身份验证的用户缓存内容

2024-02-13

对于网站,我只想为未经身份验证的用户缓存页面 - 经过身份验证的用户不会获得缓存的内容(因为他们将更新并且需要立即查看结果)。

我知道如何使用 VaryByCustom 改变每个用户的缓存:Link1 http://visitmix.com/LabNotes/Using-VaryByCustom-With-OutputCache-in-ASPNET-MVC-To-Support-Caching Link2 https://stackoverflow.com/questions/1256317/how-not-cache-an-asp-net-user-control

...但我不知道如何完全关闭经过身份验证的用户的缓存。

该怎么办?

Edit

如果已经存在来自未经身份验证的用户的页面的缓存版本,下面的代码就会出现问题。基本上,经过身份验证的用户将获得未经身份验证的事物视图。

但是,这里的链接有有效的解决方案:Link https://stackoverflow.com/questions/2109928/how-to-turn-output-caching-off-for-authenticated-users-in-asp-net-mvc


使用它作为全局操作过滤器。

public class NoCacheForAuthenticatedUsersAttribute: ActionFilterAttribute
{
    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {
        if(filterContext.HttpContext.User.Identity.IsAuthenticated)
        {
            filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        }
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

仅为未经身份验证的用户缓存内容 的相关文章

随机推荐