منع التخزين المؤقت من سمات في ASP.NET MVC, قوة السمة تنفيذ كل إجراء يتم تنفيذه

StackOverflow https://stackoverflow.com/questions/1441467

سؤال

وفقا لمختلف المواد (على سبيل المثال هنا و هنا) السمة نتائج على ASP.NET MVC الإجراءات قد تكون مؤقتا و لا أعدم مرة أخرى عندما تحكم العمل.

هذا السلوك غير المطلوب في حالتي (مثلا ، لدي نظام الترخيص بناء على سمات خاصة بها و IPs, دور الشيكات التي تحتاج إلى تنفيذ في كل مرة, و أشياء أخرى).

كيف يمكنني منع ASP.NET MVC من التخزين المؤقت بلدي سمات/السمة نتائج التنفيذ و ضمان أن تكون أعدم كل مرة?

هل كانت مفيدة؟

المحلول

نظرة على التعليمات البرمجية المصدر AuthorizeAttribute (على Codeplex أو عن طريق عاكس) لنرى كيف ستسير الامور حول إيقاف تشغيل التخزين المؤقت أذن صفحات.أنا بتعميل الترميز في طريقة منفصل على العرف تصريح السمة التي تستمد من 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 */);
}

نصائح أخرى

لقد انتهيت للتو مناقشة الحماسية مع كريغ Stuntz (المؤلف من المادة الأولى المذكورة).

انتهى بي الأمر باستخدام AuthorizeAttribute مع AuthorizeCore لضمان إذن يسمى حتى في حالة تخزين الصفحة في الذاكرة المخبئية.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top