此站点无法提供安全连接

Pra*_*eep 10 asp.net security web-config url-rewriting azure

当我在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>
Run Code Online (Sandbox Code Playgroud)

但是,当我在本地计算机上运行相同的代码时,会出现以下错误.

此站点无法提供安全连接

在此输入图像描述

在本地计算机上运行上述代码时,如何解决上述错误?

小智 15

这总是为我解决问题。

  • 在解决方案资源管理器中,单击您的项目。
  • 按 F4 键(查看属性)。
  • 复制 URL(不是 SSL URL)。
  • 将 URL 粘贴到 Web 选项卡上的项目 URL 中,然后保存。
  • 在解决方案资源管理器中,单击您的项目。
  • 按 F4 键(查看属性)。
  • 将启用 SSL 更改为 false。
  • 将其改回 true。应该有一个新的 SSL URL。复制它。
  • 将新的 SSL URL 粘贴到 Web 选项卡上的项目 URL 中。单击创建虚拟目录。
  • 单击“覆盖应用程序根 URL”,然后粘贴 SSL URL。节省。


juu*_*nas 7

我个人所做的就是将重写配置精确地放入Web.Release.config中,因为让它在本地运行有点繁琐.

问题是IIS Express会在不同的端口上公开HTTP和HTTPS,所以如果你重定向http://localhost:1234https://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>
Run Code Online (Sandbox Code Playgroud)

请注意,我还在这里添加了HSTS.它<rewrite>在发布模式下将元素插入到Web.config中.该<system.webServer>元素已存在于Web.config中,否则我将插入该元素.

  • 从项目属性更改运行 iis Express 的端口对我有用。 (2认同)