Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace SitefinityWebApp.Websilk
- {
- public class CustomPageRouteHandler : MvcPageRouteHandler
- {
- protected override bool ApplyServerCache(HttpContextBase context, OutputCacheProfileElement profile, PageSiteNode siteNode)
- {
- var allowCaching = false;
- //Never cache POST requests, and never cache if the USER isn't null
- if (context.Request.RequestType != "POST" && !ClaimsManager.GetCurrentIdentity().IsAuthenticated)
- {
- //NB: this always returns true, but maybe that changes in future?
- allowCaching = base.ApplyServerCache(context, profile, siteNode);
- //Need to use reflection as .NET does not allow for setting false value once we've set it to true
- var headers = context.Response.Cache.VaryByHeaders;
- var headersCollection = headers.GetType().GetField("_headers", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(headers) as NameObjectCollectionBase;
- if (headersCollection != null)
- {
- var method = headersCollection.GetType().GetMethod("BaseRemove", BindingFlags.NonPublic | BindingFlags.Instance);
- method.Invoke(headersCollection, new object[] { "User-Agent" });
- }
- //Extra cache callback check... It should never return cached item for logged in users
- context.Response.Cache.AddValidationCallback(IgnoreCacheRequestIfLoggedIn, null);
- }
- else
- {
- //Never cache POST or Authenticated requests
- context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
- }
- return allowCaching;
- }
- static void IgnoreCacheRequestIfLoggedIn(HttpContext context, object data, ref HttpValidationStatus status)
- {
- //Ignore cache for logged in users, but everyone else is still valid
- //NB: There's much extra logic in the base Sitefinity PageRouteHandler
- status = ClaimsManager.GetCurrentIdentity().IsAuthenticated ? HttpValidationStatus.IgnoreThisRequest : status;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment