ASP.NET HTTPS 重定向和 HSTS header

标签 asp.net azure https web-config hsts

我们有一个托管在 Azure 中的 ASP.NET 站点。

之前我们实现了以下重写规则来强制使用 HTTPS:

  <rules>
    <rule name="Redirect to https">
      <match url="(.*)"/>
      <conditions>
        <add input="{HTTPS}" pattern="Off"/>
        <add input="{REQUEST_METHOD}" pattern="^get$|^head$|^post$" />
      </conditions>
      <action type="Redirect" url="https://{HTTP_HOST}/{R:1}"/>
    </rule>
  </rules>

我们现在通过以下规则引入 HSTS(来自 this guide):

  <outboundRules>
    <rule name="Add Strict-Transport-Security only when using HTTPS" enabled="true">
      <match serverVariable="RESPONSE_Strict_Transport_Security" pattern=".*" />
      <conditions>
        <add input="{HTTPS}" pattern="on" ignoreCase="true" />
      </conditions>
      <action type="Rewrite" value="max-age=31536000; includeSubdomains; preload" />
    </rule>
  </outboundRules>

我们是否需要这两种规则,或者我们可以删除 HTTPS 规则而只使用 HSTS 规则吗?

最佳答案

不,你两者都需要。

HSTS 只能应用于符合规范的安全请求。将其应用于不安全的请求是没有意义的,因为中间人可能会删除 header 。

如果请求通过 HTTP 传入,您必须首先将其重定向到 HTTPS。然后在响应中返回 HSTS header 。

引用自规范:https://www.rfc-editor.org/rfc/rfc6797#section-7.2

An HSTS Host MUST NOT include the STS header field in HTTP responses conveyed over non-secure transport.

关于ASP.NET HTTPS 重定向和 HSTS header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44649334/

相关文章:

javascript - 在 asp.net MVC 中提交局部 View 时如何显示加载微调器?

asp.net - 如何获取当前用户主体

javascript - 在 Docker 容器内运行时,NodeJS HTTPS 服务器通过 HTTP 而不是 HTTPS 提供服务

node.js - nodejs https客户端证书未发送错误,但已发送

java - 使用HTTPS提交表单时是否需要对请求参数进行加密?

c# - 发送更大尺寸的附件 asp.net

c# - 使用无效键值设置 JArray 值 : "version". 需要 Int32 数组索引

c# - Azure Web App System.UnauthorizedAccessException 一旦部署到 Azure 就无法访问本地网络上的共享文件夹

azure - 将 SSIS (.ispac) 打包到 NuGet Feed(Azure Artifacts)中

asp.net 4.0 项目 server tag is not wellformed 编译错误