此站点无法提供安全连接
This site can’t provide a secure connection
当我在 web.config 中添加 URL 重写代码,然后将其发布到 Azure 中。即使我尝试使用 http 访问网站,它也会自动重定向到 https。
<rewrite>
<rules>
<rule name="Redirect to https">
<match url="(.*)"/>
<conditions>
<add input="{HTTPS}" pattern="Off"/>
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}"/>
</rule>
</rules>
</rewrite>
但是当我 运行 在我的本地机器上使用相同的代码时,会出现以下错误。
此站点无法提供安全连接
当我在本地机器上 运行 上面的代码时,如何解决上面的错误?
您必须配置 Visual Studio 服务器才能与 HTTPS 一起使用。
请通过此 link 了解详细信息:
我个人所做的就是将重写配置放入 Web.Release.config 正是因为让它在本地工作有点繁琐。
问题是 IIS Express 会在不同的端口上公开 HTTP 和 HTTPS,因此如果您从 http://localhost:1234
重定向到 https://localhost:1234
,它根本无法工作,因为 IIS Express 正在公开 HTTPS在 https://localhost:44300
.
之类的东西上
您可以在 IIS Express 上启用 SSL/TLS(您应该这样做),但我只为发布模式保留重写规则。
这里是一个例子 Web.Release.config 文件:
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<system.web>
<compilation xdt:Transform="RemoveAttributes(debug)" />
</system.web>
<system.webServer>
<rewrite xdt:Transform="Insert">
<rules>
<!-- Redirects users to HTTPS if they try to access with HTTP -->
<rule
name="Force HTTPS"
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>
<!-- Enforces HTTPS for browsers with HSTS -->
<!-- As per official spec only sent when users access with HTTPS -->
<rule
xdt:Transform="Insert"
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>
</configuration>
注意我这里也加了HSTS。它在 Release 模式下将 <rewrite>
元素插入到 Web.config 中。 <system.webServer>
元素已经存在于 Web.config 中,否则我会插入它。
我用旧版本的 Chrome 网络浏览器解决了这个问题。
这是 list of older chrome versions 您可以下载并安装它的地方。
60.0.3112.90 - Ubuntu 是适合我的版本。
也许它比新版本慢一点,但我发现它非常适合生产 (:
在我这边,我发现有一个 javascript 代码可以将网站从 http 重定向到 https。因此,如果有其他代码导致该问题,请尝试探索您的环境。希望这能有所帮助。谢谢
这总能解决我的问题。
- 在解决方案资源管理器中,单击您的项目。
- 按 F4 键(查看属性)。
- 复制 URL(不是 SSL URL)。
- 将 URL 粘贴到 Web 选项卡上的项目 Url 中,保存。
- 在解决方案资源管理器中,单击您的项目。
- 按 F4 键(查看属性)。
- 将启用 SSL 更改为 false。
- 将其改回 true。应该有一个新的 SSL URL。复制它。
- 将新的 SSL URL 粘贴到 Web 选项卡上的项目 URL 中。单击创建虚拟目录。
- 单击覆盖应用程序根 URL,然后粘贴 SSL URL。保存。
当我在 web.config 中添加 URL 重写代码,然后将其发布到 Azure 中。即使我尝试使用 http 访问网站,它也会自动重定向到 https。
<rewrite>
<rules>
<rule name="Redirect to https">
<match url="(.*)"/>
<conditions>
<add input="{HTTPS}" pattern="Off"/>
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}"/>
</rule>
</rules>
</rewrite>
但是当我 运行 在我的本地机器上使用相同的代码时,会出现以下错误。
此站点无法提供安全连接
当我在本地机器上 运行 上面的代码时,如何解决上面的错误?
您必须配置 Visual Studio 服务器才能与 HTTPS 一起使用。
请通过此 link 了解详细信息:
我个人所做的就是将重写配置放入 Web.Release.config 正是因为让它在本地工作有点繁琐。
问题是 IIS Express 会在不同的端口上公开 HTTP 和 HTTPS,因此如果您从 http://localhost:1234
重定向到 https://localhost:1234
,它根本无法工作,因为 IIS Express 正在公开 HTTPS在 https://localhost:44300
.
您可以在 IIS Express 上启用 SSL/TLS(您应该这样做),但我只为发布模式保留重写规则。
这里是一个例子 Web.Release.config 文件:
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<system.web>
<compilation xdt:Transform="RemoveAttributes(debug)" />
</system.web>
<system.webServer>
<rewrite xdt:Transform="Insert">
<rules>
<!-- Redirects users to HTTPS if they try to access with HTTP -->
<rule
name="Force HTTPS"
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>
<!-- Enforces HTTPS for browsers with HSTS -->
<!-- As per official spec only sent when users access with HTTPS -->
<rule
xdt:Transform="Insert"
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>
</configuration>
注意我这里也加了HSTS。它在 Release 模式下将 <rewrite>
元素插入到 Web.config 中。 <system.webServer>
元素已经存在于 Web.config 中,否则我会插入它。
我用旧版本的 Chrome 网络浏览器解决了这个问题。
这是 list of older chrome versions 您可以下载并安装它的地方。
60.0.3112.90 - Ubuntu 是适合我的版本。
也许它比新版本慢一点,但我发现它非常适合生产 (:
在我这边,我发现有一个 javascript 代码可以将网站从 http 重定向到 https。因此,如果有其他代码导致该问题,请尝试探索您的环境。希望这能有所帮助。谢谢
这总能解决我的问题。
- 在解决方案资源管理器中,单击您的项目。
- 按 F4 键(查看属性)。
- 复制 URL(不是 SSL URL)。
- 将 URL 粘贴到 Web 选项卡上的项目 Url 中,保存。
- 在解决方案资源管理器中,单击您的项目。
- 按 F4 键(查看属性)。
- 将启用 SSL 更改为 false。
- 将其改回 true。应该有一个新的 SSL URL。复制它。
- 将新的 SSL URL 粘贴到 Web 选项卡上的项目 URL 中。单击创建虚拟目录。
- 单击覆盖应用程序根 URL,然后粘贴 SSL URL。保存。