无法使用PreSendRequestHeaders()在IIS中覆盖http缓存标头

5

历史背景:
由于安全考虑,我们的组织希望通过向IIS添加HTTP头来禁用缓存。

过期时间:-1
Pragma: no-cache
Cache Control: No-cache, No-store

添加这些头会导致IE6中SSL的"application/vnd.ms-excel"响应类型失败。微软承认这是一个bug (http://support.microsoft.com/kb/323308),他们的解决方案也有效。然而,这个解决方案必须作为整个组织的补丁推出,这在高层管理人员中会遇到一定的阻力。

问题:
与此同时,我们正在尝试通过在PreSendRequestHeaders()函数上使用HTTPModules覆盖IIS设置的HTTP头来寻找替代方法,针对具有MIME类型“application/vnd.ms-excel”的页面。

//this is just a sample code
public void Init(HttpApplication context)
        {
            context.PreSendRequestHeaders += new EventHandler(context_PreSendRequestHeaders);

        }
protected void context_PreSendRequestHeaders(object sender, EventArgs e) 
        {
            HttpApplication application = (HttpApplication)sender;
            if(application.Response.ContentType == "application/vnd.ms-excel; name=DataExport.xls")
            {
                application.Response.ClearHeaders();
                application.Response.ContentType = "application/vnd.ms-excel; name=DataExport.xls";
                application.Response.AddHeader("Content-Transfer", "Encoding: base64");
                application.Response.AddHeader("Content-Disposition", "attachment;filename=DataExport.xls");
                application.Response.AddHeader("cache-control","private");
            }
        }

即使使用了ClearHeaders()方法清除了头部信息,IIS发送响应前仍会附加缓存头信息。
问题: 在PreSendRequestHeaders()函数中使用ClearHeaders()方法的做法是否错误? ASP.NET 1.1中是否有其他可用库来覆盖缓存头信息(Expires, Pragma, cache-control)?
其他信息: 我们正在使用: 浏览器:IE6 SP 3 服务器:IIS 6 平台:.NET 1.1
2个回答

2
使用IIS 7.5+和URL Rewrite扩展,添加一个出站规则来删除Cache-Control头中的"no-store"值和Pragma头,可以更轻松地实现此操作。以下是该规则集:
<outboundRules>
    <rule name="Always Remove Pragma Header">
        <match serverVariable="RESPONSE_Pragma" pattern="(.*)" />
        <action type="Rewrite" value="" />
    </rule>
    <rule name="Remove No-Store for Attachments">
        <conditions>
            <add input="{RESPONSE_Content-Disposition}" pattern="attachment" />
        </conditions>
        <match serverVariable="RESPONSE_Cache-Control" pattern="no-store" />
        <action type="Rewrite" value="max-age=0" />
    </rule>
</outboundRules>

1
请查看:

IIS7 + ASP.NET MVC中未向客户端浏览器发送Cache-control: no-store, must-revalidate

您必须在PreSendRequestHeaders处理程序内使用以下调用序列来正确设置无缓存标头,否则Cache-Control标头稍后会被覆盖:

Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.Cache.AppendCacheExtension("no-store, must-revalidate"); Response.AppendHeader("Pragma", "no-cache"); Response.AppendHeader("Expires", "0");


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接