outputcache mvc3只注销了用户缓存

时间:2012-03-28 05:55:30

标签: asp.net-mvc-3 outputcache

有没有办法使用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
}

2 个答案:

答案 0 :(得分:8)

您可以使用VaryByCustom中的[OutputCache]媒体资源。

然后覆盖HttpApplication.GetVaryByCustomString并检查HttpContext.Current.User.IsAuthenticated

  • 如果未经过身份验证(激活缓存),则返回"NotAuthed"或类似内容
  • Guid.NewGuid().ToString()使缓存无效

答案 1 :(得分:4)

这是我实现上述目的的方式。

在Global.asax.cs中:

public override string GetVaryByCustomString(HttpContext context, string custom)
{
    if (custom == "UserId")
    {
        if (context.Request.IsAuthenticated)
        {
            return context.User.Identity.Name;
        }
        return null;
    }

    return base.GetVaryByCustomString(context, custom);
}

输出缓存属性中的用法:

 [OutputCache(Duration = 30, VaryByCustom = "UserId" ...
 public ActionResult MyController()
 {
    ...
相关问题