我们的调查表明,并非所有浏览器都以统一的方式尊重HTTP缓存指令。

出于安全原因,我们不希望应用程序中的某些页面被web浏览器缓存。这必须至少适用于以下浏览器:

Internet Explorer 6+Firefox 1.5+Safari 3+歌剧9+铬

我们的要求来自安全测试。从我们的网站注销后,您可以按后退按钮并查看缓存的页面。


当前回答

除了标题之外,还可以考虑通过https为页面提供服务。许多浏览器默认不会缓存https。

其他回答

免责声明:我强烈建议阅读@BalusC的答案。阅读以下缓存教程后:http://www.mnot.net/cache_docs/(我建议你也读一下),我相信这是正确的。然而,出于历史原因(并且因为我自己测试过),我将在下面附上我的原始答案:


我尝试了PHP的“接受”答案,但这对我来说不起作用。然后我做了一些研究,发现了一个轻微的变体,对其进行了测试,结果成功了。这里是:

header('Cache-Control: no-store, private, no-cache, must-revalidate');     // HTTP/1.1
header('Cache-Control: pre-check=0, post-check=0, max-age=0, max-stale = 0', false);  // HTTP/1.1
header('Pragma: public');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');                  // Date in the past  
header('Expires: 0', false); 
header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT');
header ('Pragma: no-cache');

这应该奏效。问题是,当两次设置头的相同部分时,如果没有将false作为第二个参数发送给头函数,头函数将简单地覆盖前面的header()调用。因此,在设置缓存控件时,例如,如果不想将所有参数放在一个header()函数调用中,则必须执行以下操作:

header('Cache-Control: this');
header('Cache-Control: and, this', false);

请在此处查看更完整的文档。

此外,如果您使用.htaccess文件启用缓存,请确保重置该文件中的ExpiresDefault。

ExpiresDefault "access plus 0 seconds"

然后,可以使用ExpiresByType为要缓存的文件设置特定值:

ExpiresByType image/x-icon "access plus 3 month"

如果您的动态文件(如php等)被浏览器缓存,并且您无法找出原因,那么这可能也很有用。选中ExpiresDefault。

在我的情况下,我用这个解决了chrome中的问题

<form id="form1" runat="server" autocomplete="off">

当用户出于安全原因单击“返回”按钮时,我需要清除先前表单数据的内容

对于ASP.NET Core,创建一个简单的中间件类:

public class NoCacheMiddleware
{
    private readonly RequestDelegate m_next;

    public NoCacheMiddleware( RequestDelegate next )
    {
        m_next = next;
    }

    public async Task Invoke( HttpContext httpContext )
    {
        httpContext.Response.OnStarting( ( state ) =>
        {
            // ref: http://stackoverflow.com/questions/49547/making-sure-a-web-page-is-not-cached-across-all-browsers
            httpContext.Response.Headers.Append( "Cache-Control", "no-cache, no-store, must-revalidate" );
            httpContext.Response.Headers.Append( "Pragma", "no-cache" );
            httpContext.Response.Headers.Append( "Expires", "0" );
            return Task.FromResult( 0 );
        }, null );

        await m_next.Invoke( httpContext );
    }
}

然后在Startup.cs中注册

app.UseMiddleware<NoCacheMiddleware>();

确保在以下位置添加此

app.UseStaticFiles();

通过设置Pragma:无缓存