我们的调查表明,并非所有浏览器都以统一的方式尊重HTTP缓存指令。
出于安全原因,我们不希望应用程序中的某些页面被web浏览器缓存。这必须至少适用于以下浏览器:
Internet Explorer 6+Firefox 1.5+Safari 3+歌剧9+铬
我们的要求来自安全测试。从我们的网站注销后,您可以按后退按钮并查看缓存的页面。
我们的调查表明,并非所有浏览器都以统一的方式尊重HTTP缓存指令。
出于安全原因,我们不希望应用程序中的某些页面被web浏览器缓存。这必须至少适用于以下浏览器:
Internet Explorer 6+Firefox 1.5+Safari 3+歌剧9+铬
我们的要求来自安全测试。从我们的网站注销后,您可以按后退按钮并查看缓存的页面。
当前回答
接受的答案似乎不适用于IIS7+,因为II7中存在大量关于未发送缓存头的问题:
某些事情正在迫使响应具有缓存控制:IIS7中的私有IIS7:缓存设置不工作。。。为什么?IIS7+ASP.NET MVC客户端缓存标头不工作为aspx页面设置缓存控制缓存控制:没有存储,必须重新验证,不能发送到IIS7+ASP.NET MVC中的客户端浏览器
等等
接受的答案是正确的,必须在哪些标题中设置,但不能在如何设置标题中设置。这种方式适用于IIS7:
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.AppendCacheExtension("no-store, must-revalidate");
Response.AppendHeader("Pragma", "no-cache");
Response.AppendHeader("Expires", "-1");
第一行将Cache控件设置为no Cache,第二行添加其他属性no store,must revalidate
其他回答
通过设置Pragma:无缓存
我用这种方式解决了。
2个注意事项:
1) 服务器端事件不是在后退单击时触发的,而是javascript。
2) 我有两个javascript来读/写cookie
function setCookie(name, value, days)
{
var expires = "";
if (days)
{
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toUTCString();
}
document.cookie = name + "=" + (value || "") + expires + "; path=/";
}
function getCookie(name)
{
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for (var i = ca.length - 1; i >= 0; i--)
{
var c = ca[i];
while (c.charAt(0) == ' ')
{
c = c.substring(1, c.length);
}
if (c.indexOf(nameEQ) == 0)
{
return c.substring(nameEQ.length, c.length);
}
}
return null;
}
在我的Page_Load中,我插入了以下内容:(这不是在单击后启动的)
protected void Page_Load(object sender, EventArgs e)
{
Page.RegisterClientScriptBlock("", "<script>setCookie('" + Session.SessionID + "', '" + Login + "', '100');</script>");
}
其中“Login”是我的id值,注销后为-1(可以使用其他值,例如布尔值)。
然后在我的页面中添加了以下内容:(这是在单击后启动的)
<script type="text/javascript">
if (getCookie('<%= Session.SessionID %>') < 0)
{
if (history.length > 0)
{
history.go(+1);
}
}
</script>
没有别的。
使用此解决方案,仅在同一浏览器上的每个页面注销后,才在每个页面上启用反向单击。
//In .net MVC
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
public ActionResult FareListInfo(long id)
{
}
// In .net webform
<%@ OutputCache NoStore="true" Duration="0" VaryByParam="*" %>
完成BalusC->答案如果您使用的是perl,则可以使用CGI添加HTTP标头。
使用Perl:
Use CGI;
sub set_new_query() {
binmode STDOUT, ":utf8";
die if defined $query;
$query = CGI->new();
print $query->header(
-expires => 'Sat, 26 Jul 1997 05:00:00 GMT',
-Pragma => 'no-cache',
-Cache_Control => join(', ', qw(
private
no-cache
no-store
must-revalidate
max-age=0
pre-check=0
post-check=0
))
);
}
使用apachehttpd.conf
<FilesMatch "\.(html|htm|js|css|pl)$">
FileETag None
<ifModule mod_headers.c>
Header unset ETag
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
</ifModule>
注意:当我尝试使用html META时,浏览器忽略了它们并缓存了页面。
不确定我的答案听起来是否简单和愚蠢,也许你早就知道了,但由于阻止某人使用浏览器后退按钮查看历史页面是你的目标之一,你可以使用:
window.location.replace(“https://www.example.com/page-not-to-be-viewed-in-browser-history-back-button.html");
当然,这可能不可能在整个网站上实现,但至少对于一些关键页面,您可以做到这一点。希望这有帮助。