gpt4 book ai didi

ASP.NET HTTPS 重定向和 HSTS header

转载 作者:行者123 更新时间:2023-12-03 00:50:54 25 4
gpt4 key购买 nike

我们有一个托管在 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 responsesconveyed over non-secure transport.

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com