.net - HTTPS 重定向在本地有效,但在 azure 上无效

标签 .net azure ssl https azure-web-app-service

这个问题可能被问了一千遍,但我仍然找不到解决方案..

我有一个 .NET 应用程序,在本地运行时,它会从 HTTP 重定向到 HTTPS。但是,当部署在 Azure Web 应用程序中时,重定向将不起作用。

根据多个论坛帖子,我的重定向是通过 Web.config 文件中的规则完成的。下面,您会看到该文件部署在 azure 上(我使用 ftp 检查过)

  <system.webServer>
    <staticContent>
      <remove fileExtension=".woff" />
      <mimeMap fileExtension=".woff" mimeType="application/font-woff" />
      <remove fileExtension=".woff2" />
      <mimeMap fileExtension=".woff2" mimeType="font/woff2" />
      <remove fileExtension=".json" />
      <mimeMap fileExtension=".json" mimeType="application/json" />
    </staticContent>
    <handlers>
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <remove name="OPTIONSVerbHandler" />
      <remove name="TRACEVerbHandler" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
    <rewrite>
      <rules>
        <rule name="HTTP to HTTPS redirect" stopProcessing="true">
          <match url="(.*)" />
          <conditions>
            <add input="{HTTPS}" pattern="off" ignoreCase="true" />
            <add input="{HTTP_HOST}" pattern="([^/:]*?):[^/]*?" />
          </conditions>
          <action type="Redirect" url="https://{C:1}/{R:1}" redirectType="Permanent" />
        </rule>
        <rule name="static dist files" stopProcessing="true">
          <match url="^(.+)$" />
          <conditions>
            <add input="{APPL_PHYSICAL_PATH}dist\{R:1}" matchType="IsFile" />
          </conditions>
          <action type="Rewrite" url="/dist/{R:1}" />
        </rule>
        <rule name="index.html as document root" stopProcessing="true">
          <match url="^$" />
          <action type="Rewrite" url="/dist/index.html" />
        </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>

Web.config 文件是对原始 Web.config 文件进行转换的结果,因为在本地,HTTPS 的端口设置为 44362,因此本地使用的 Web.config 有点不同:

    <rule name="HTTP to HTTPS redirect" stopProcessing="true">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTPS}" pattern="off" ignoreCase="true" />
        <add input="{HTTP_HOST}" pattern="([^/:]*):[^/]*?" />
      </conditions>
      <action type="Redirect" url="https://{C:1}:44362/{R:1}"
          redirectType="Permanent" />
    </rule>

在本地,一切正常。重定向瞬间完成。 但是当部署在 azure Web 应用程序中时,不会发生重定向。 因此“---.azurewebsites.net”不会自动重定向到“https://---.azurewebsites.net

我可以看到,他达到了规则,因为浏览器选项卡的标题瞬间以该规则的名称重命名。

如果有人发现此代码中的愚蠢错误,请告诉我。

问候,

最佳答案

Azure 应用服务中提供了一个扩展,可以自动为你执行此操作。

这些方向可能会随着 Azure 门户的变化而变化,但截至 2018 年 5 月为止都是最新的。

  1. 转到 Azure 门户中的应用服务。
  2. 点击顶部菜单中的“工具”。
  3. 在“开发”部分中,点击“扩展”。
  4. 在“扩展程序”边栏选项卡中,点击顶部的“添加”。
  5. 在“选择扩展”边栏选项卡中,选择“将 HTTP 重定向到 HTTPS”。
  6. 单击“确定”或其他任何按钮,直至保存。

之后,您的所有 HTTP 流量都将重定向到 HTTPS,无需进行任何其他更改。

关于.net - HTTPS 重定向在本地有效,但在 azure 上无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38915505/

相关文章:

azure - 尝试向用户添加架构扩展时权限不足

Azure 指标表与 Azure 仪表板中的指标

Azure 不会覆盖应用程序设置

java - Java 7 中的客户端 TLS session 票证支持

JBoss 的 SSL 证书导入和配置

c# - 在不同的 Entity Framework 模型中不能有相同的表名吗?

c# - 如何在C#或CS1503中使用通用变量

c# - Silverlight CustomControl 依赖属性不能绑定(bind)到父 View 模型

c# - HTML编码解码c#MVC4

python3 和请求 : still getting 'sslv3 alert handshake failure'