asp.net - 此站点无法提供安全连接 (ERR_SSL_PROTOCOL_ERROR)

标签 asp.net security azure url-rewriting web-config

当我在 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>

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

此网站无法提供安全连接

enter image description here

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

最佳答案

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

问题在于 IIS Express 将在不同端口上公开 HTTP 和 HTTPS,因此如果您从 http://localhost:1234 重定向至https://localhost:1234 ,它根本不起作用,因为 IIS Express 在类似 https://localhost:44300 上公开 HTTPS .

您可以在 IIS Express 上启用 SSL/TLS(您应该这样做),但我只会为 Release模式保留重写规则。

这是一个示例 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。它插入 <rewrite>在 Release模式下将元素添加到 Web.config 中。 <system.webServer>元素已存在于 Web.config 中,否则我将插入该元素。

关于asp.net - 此站点无法提供安全连接 (ERR_SSL_PROTOCOL_ERROR),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43338665/

相关文章:

c# - 如何在服务器端为 C#/ASP.NET 中的图像控件添加单击事件

asp.net - 如何删除 CreateUserWizard 控件中的表标签

c# - asp.net 在新页面中查看 pdf

系统应用程序的 Android java.lang.SecurityException

php - symfony2 调用 is_granted in voter : how to avoid an infinite loop?

facebook - 我可以要求 Canvas URL 使用 HTTPS 来保护signed_request 内容吗?

.net - Microsoft Azure 存储与 Azure SQL 数据库

rest - 如何在没有用户交互的情况下向 Azure Active Directory 进行身份验证?

javascript - 如何在 JavaScript 中从 GridView 获取值

azure - 使用自定义 URL 访问 Azure WebApp 会产生 "too many redirects"错误