Prevent Caching of Attributes in ASP.NET MVC, force Attribute Execution every time an Action is Executed

后端 未结 2 683
一个人的身影
一个人的身影 2021-01-03 03:59

According to various articles (e.g. here and here) attribute results on ASP.NET MVC Actions may be cached and not executed again when a con

相关标签:
2条回答
  • 2021-01-03 04:27

    Look at the source code for the AuthorizeAttribute (on Codeplex or via Reflector) to see how it goes about turning off caching for authorized pages. I refactored it into a separate method on my custom authorization attribute which derives from AuthorizeAttribute.

    protected void CacheValidateHandler( HttpContext context, object data, ref HttpValidationStatus validationStatus )
    {
        validationStatus = OnCacheAuthorization( new HttpContextWrapper( context ) );
    }
    
    protected void SetCachePolicy( AuthorizationContext filterContext )
    {
        // ** IMPORTANT **
        // Since we're performing authorization at the action level, the authorization code runs
        // after the output caching module. In the worst case this could allow an authorized user
        // to cause the page to be cached, then an unauthorized user would later be served the
        // cached page. We work around this by telling proxies not to cache the sensitive page,
        // then we hook our custom authorization code into the caching mechanism so that we have
        // the final say on whether a page should be served from the cache.
        HttpCachePolicyBase cachePolicy = filterContext.HttpContext.Response.Cache;
        cachePolicy.SetProxyMaxAge( new TimeSpan( 0 ) );
        cachePolicy.AddValidationCallback( CacheValidateHandler, null /* data */);
    }
    
    0 讨论(0)
  • 2021-01-03 04:29

    I just finished a spirited discussion with Craig Stuntz (the author of the first article you listed).

    I ended up using an AuthorizeAttribute with AuthorizeCore to guarantee that authorization is called even in the event the page is cached.

    0 讨论(0)
提交回复
热议问题