有没有办法使用OutputCache属性来缓存仅注销用户的结果并重新评估登录用户示例:
我想要什么
- [OutputCache(onlycacheanon = true)]
- public ActionResult GetPhoto(id){
- var photo = getPhoto(id);
- if(!photo.issecured){
- return photo...
- }
- return getPhotoOnlyIfCurrentUserHasAccess(id);
- //otherwise return default photo so please don't cache me
- }
解决方法
您可以在[OutputCache]中使用VaryByCustom属性.
然后覆盖HttpApplication.GetVaryByCustomString并检查HttpContext.Current.User.IsAuthenticated.
>如果未经过身份验证,则返回“NotAuthed”或类似内容(激活缓存)> Guid.NewGuid().ToString()使缓存无效