Updated the DefaultAntiforgery to set the the cache headers only if they aren't set yet.

This commit is contained in:
Artak Mkrtchyan 2018-01-22 14:50:45 -08:00
parent e5de4e672c
commit 6138087de6
1 changed files with 26 additions and 11 deletions

View File

@ -373,14 +373,29 @@ namespace Microsoft.AspNetCore.Antiforgery.Internal
/// </summary> /// </summary>
/// <param name="httpContext">The <see cref="HttpContext"/>.</param> /// <param name="httpContext">The <see cref="HttpContext"/>.</param>
protected virtual void SetDoNotCacheHeaders(HttpContext httpContext) protected virtual void SetDoNotCacheHeaders(HttpContext httpContext)
{
bool cacheHeadersChanged = SetHeaderIfNotSet(httpContext, HeaderNames.CacheControl, "no-cache, no-store");
cacheHeadersChanged |= SetHeaderIfNotSet(httpContext, HeaderNames.Pragma, "no-cache");
if (cacheHeadersChanged)
{ {
// Since antifogery token generation is not very obvious to the end users (ex: MVC's form tag generates them // Since antifogery token generation is not very obvious to the end users (ex: MVC's form tag generates them
// by default), log a warning to let users know of the change in behavior to any cache headers they might // by default), log a warning to let users know of the change in behavior to any cache headers they might
// have set explicitly. // have set explicitly.
LogCacheHeaderOverrideWarning(httpContext.Response); LogCacheHeaderOverrideWarning(httpContext.Response);
}
}
httpContext.Response.Headers[HeaderNames.CacheControl] = "no-cache, no-store"; private static bool SetHeaderIfNotSet(HttpContext context, string headerName, string value)
httpContext.Response.Headers[HeaderNames.Pragma] = "no-cache"; {
if (!context.Response.Headers.ContainsKey(headerName))
{
context.Response.Headers[headerName] = value;
return true;
}
return false;
} }
private void LogCacheHeaderOverrideWarning(HttpResponse response) private void LogCacheHeaderOverrideWarning(HttpResponse response)