【问题标题】:Strip off Cache-control and Pragma: no-cache http headers without using an Http Module剥离 Cache-control 和 Pragma: no-cache http headers without using an Http Module
【发布时间】:2014-01-03 11:33:09
【问题描述】:

问题: 我们有一个 .aspx 又名 WebForms 页面,使用户能够将显示的报告下载为 MS-Excel 下载。此页面作为 Web 应用程序的一部分以及桌面应用程序的浏览器框架 (ieframe) 中提供。

  1. 在所有浏览器以及浏览器框架内都可以通过 HTTP 正常下载
  2. 当我们切换到 HTTPS(在生产环境中)时,下载拒绝工作

这个SO Question详细说明问题和原因。

由于解决方案涉及剥离 Cache-control: no-cache 和 Pragma: no-cache,我编写了一个 Http 模块来实现这一点。

建议的解决方案

HttpModule 基本上是使用 PreSendRequestHeaders 事件来完成的:

private void OnPreSendRequestHeaders(object sender, EventArgs e)
        {
            if (null == _httpApplication)
            {
                return;
            }

            if (_httpApplication.Context != null)
            {
                var response = _httpApplication.Response;
                if (_httpApplication.Request.Url.AbsoluteUri.ToLowerInvariant().Contains("mypage.aspx"))
                {
                    HeadersToCloak.ForEach(header => response.Headers.Remove(header));
                    response.Headers.Add("Cache-Control", "private, max-age=15");
                }
            }
        }

争论的焦点: 在代码审查期间,我被告知这不是一个好的解决方案,因为我的自定义模块将针对所有请求运行,因此会对性能产生影响。 应用程序的 web.config 在 system.webServer 节点下已 runAllManagedModulesForAllRequests="true",这是应用程序所需的其他功能所必需的。

我的尝试

  1. 尝试将用于删除和添加标题的代码放置在 ASPX 页面本身的几个事件中(一个接一个),包括 RenderComplete 但是当我检查 Fiddler 中的响应时,麻烦的标题仍然存在(没有替换为预期的标题)
  2. 我已经按照HERE 的说明查找了 preCondition 标记,但由于我们在集成管道模式下运行并且已 runAllManagedModulesForAllRequests="true",因此 preCondition 将毫无意义

需要帮助的问题:

Q1) 鉴于该模块将针对所有请求(包括静态文件)运行,并且我仅在请求的 URI 用于相关页面时才进行实际工作,那么运行此模块对性能有多大影响?

Q2) 我怎样才能删除和设置仅一页的标题?

Q3) 鉴于限制,是否可以仅针对托管请求或仅针对我的页面运行此模块(不认为后者是可能的)?

【问题讨论】:

    标签: asp.net https webforms http-headers iis-7.5


    【解决方案1】:

    商定的解决方案如下:

    1) 将需要去除标题功能的页面集移动到根应用程序下各自的单独文件夹中

    2) 在此文件夹中放置一个 web.config,在 system.webServer 下设置如下:

      <system.webServer>
        <modules runAllManagedModulesForAllRequests="true">
          <remove name="HeaderStripModule" />
          <add name="HeaderStripModule" type="Com.Reports.HeaderStripModule" />
        </modules>
      </system.webServer>
    

    在根 web.config 中,放置此设置:

      <system.webServer>
        <modules runAllManagedModulesForAllRequests="true">
          <remove name="HeaderStripModule" />          
        </modules>
      </system.webServer>
    

    在这些更改之后,我通过 fiddler 进行了验证,发现该模块(预期)仅针对文件夹内的页面(在 IIS 下设置为应用程序)运行。

    【讨论】:

    • 事实证明,此配置要求您的文件夹是 IIS 中的应用程序,并且在尝试加载程序集时会出现其他问题。此 SO 答案中记录了更好的解决方案:stackoverflow.com/questions/2378586/… [查看我对已接受答案的评论]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-17
    • 2017-02-03
    • 1970-01-01
    • 2017-10-09
    • 2011-01-25
    • 2021-04-19
    • 2011-11-26
    相关资源
    最近更新 更多