gpt4 book ai didi

c# - 实现 HSTS 时出现 SSL 错误

转载 作者:太空宇宙 更新时间:2023-11-03 13:16:37 24 4
gpt4 key购买 nike

我正在尝试在我的网站(例如 www.example.com)上实现 HSTS,该网站最近已从 http 移至 https,并且位于默认端口。我有一个 WCF 服务运行在同一个域但不同的端口(例如 www.example.com:8000)。

我尝试做的是在 Web.config 文件中添加以下代码

<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Strict-Transport-Security" value="max-age=31536000"/>
</customHeaders>
</httpProtocol>
</system.webServer>

后来我尝试了来自 here 的这段代码(因为它说上面的实现是不正确的)

<system.webServer>
<rewrite>
<rules>
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}"
redirectType="Permanent" />
</rule>
</rules>
<outboundRules>
<rule name="Add Strict-Transport-Security when 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" />
</rule>
</outboundRules>
</rewrite>
</system.webServer>

我的网站 www.example.com 运行良好。但是一旦我访问 www.example.com,响应 header Strict-Transport-Security 就会被缓存,然后尝试访问 WCF 服务页面会重定向到 https,因为该服务正在 8000 端口上运行,导致“SSL 错误”。

注意:清除缓存后访问同一个服务页面显示页面。

如何停止将端口 8000 上的调用重定向到 https?

最佳答案

HSTS 适用于域上的所有 HTTP 调用,而不仅仅是端口 80 上的调用。

来自rfc :

The UA MUST replace the URI scheme with "https" [RFC2818], and

if the URI contains an explicit port component of "80", then the UAMUST convert the port component to be "443", or

if the URI contains an explicit port component that is not equal to"80", the port component value MUST be preserved; otherwise,

if the URI does not contain an explicit port component, the UA MUST NOT add one.

NOTE: These steps ensure that the HSTS Policy applies to HTTP over any TCP port of an HSTS Host.

因此您需要执行以下操作之一:

  1. 也将您的 WCF 服务切换到 https。
  2. 切换 WCF 服务的域,使其不受 HSTS 政策的影响。
  3. 停止使用 HSTS。

关于c# - 实现 HSTS 时出现 SSL 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33873283/

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