c# - 当托管在 Azure 应用服务中时,如何控制添加到 ASP.NET Core Web 应用程序的 HTTP 响应 header ?

标签 c# azure http asp.net-core azure-web-app-service

我们编写了一个 ASP.NET core 2.2 Web 应用程序,它基本上公开了一些 Web api Controller ,并且我们使用了 ResponseCachingMiddleware为了在我们的中间件管道中实现服务器端响应缓存。

我们关注了this Microsoft guide我们决定添加 HTTP 响应 header Vary,以便应用程序的每个响应都包含以下 header :Vary: Accept-Encoding, Accept-Charset

按照上面链接的指南中的说明,需要这样做,以便响应缓存能够遵守客户端请求 header ,以便当且仅当缓存的响应与客户端请求兼容时才使用它们。

与 postman 进行测试时,我注意到,在 Azure 中部署应用程序时(我们使用标准的 Azure 应用服务来执行此操作),Vary 响应 header 不是我所期望的:看起来 Azure 本身添加了值 Accept-Encoding,以便将 Vary header 的值设置为 Accept-Encoding、Accept-Charset、Accept-Encoding(这是我们的应用程序设置的值和我想,该值是由 Azure 自动添加的)。

也就是说我有几个问题:

  • 额外的值Accept-Encoding真的是由azure主机添加的吗?
  • 有没有办法自定义 Azure 主机添加的 HTTP header (如果有)?
  • Accept-Encoding、Accept-Charset、Accept-Encoding 值是 Vary header 的有效值吗?即使我们有一个值重复两次,它也会按预期工作吗?

最佳答案

在 Azure 应用服务(Windows 应用服务)上托管 ASP .NET Core 仍使用 IIS,如所述 here 。因此,您应该能够通过将 web.config 添加到项目中来控制 header 。

下面是一个示例,显示 link到文档,

<configuration>  
  <system.web>
    <httpRuntime enableVersionHeader="false" /> <!-- Removes ASP.NET version header. Not needed for Ghost running in iisnode -->
  </system.web>
  <system.webServer>
    <security>
      <requestFiltering removeServerHeader="true" /> <!-- Removes Server header in IIS10 or later and also in Azure Web Apps -->
    </security>
    <httpProtocol>
      <customHeaders>
        <clear /> <!-- Gets rid of the other unwanted headers -->
        <add name="X-Frame-Options" value="SAMEORIGIN" />
      </customHeaders>
      <redirectHeaders>
        <clear />
      </redirectHeaders>
    </httpProtocol>

关于c# - 当托管在 Azure 应用服务中时,如何控制添加到 ASP.NET Core Web 应用程序的 HTTP 响应 header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55341080/

相关文章:

C# 读取字符串中的多个标签

c# - 使用WCF服务返回MembershipUser

Azure Web部署失败

android - java.lang.NoClassDefFoundError : Failed resolution of: Lorg/apache/http/params/BasicHttpParams;

java - ElasticSearch 和 Apache HttpAsyncClient

node.js - 动态注入(inject)脚本标签pre </body>

C# 如何创建自定义类,如 "cmd.CommandType = System.Data.CommandType.Text"

c# - 相同的声音重叠,因此声音很大。提示与技巧?

azure - 在 Azure 中删除之前在 VM 上运行脚本

Windows Azure : how to measure the execution time of a code