【问题标题】:Azure Web App - redirect all traffic from http to httpsAzure Web App - 将所有流量从 http 重定向到 https
【发布时间】:2017-09-22 16:48:55
【问题描述】:

我们有一个 Asp.Net Core Web 应用程序,它在 .Net 框架 (net452) 上运行,并作为 Web 应用程序托管在 Azure 中。

我正在尝试将所有 http 请求重定向到 https。

我目前的理解是:

  1. 我无法将 web.config 中的 IIS 重写规则指定为应用程序 是一个 Asp.Net Core 应用程序
  2. 我无法使用 Microsoft.AspNetCore.Rewrite 中间件版本为 2.0.0.0 它 需要.Net Standard 2.0,net452 只支持.Net Standard 1.5.

如果以上是正确的,那么最好的方法是什么?

我目前正在考虑编写一小块中间件,但感觉必须有更简单的方法......

【问题讨论】:

    标签: asp.net azure redirect https


    【解决方案1】:

    我真的怀疑第 1 点。 因为它是 IIS 设置。 <system.webServer> 下的任何内容仅与 IIS 相关,而不与您正在使用的技术相关,即使使用 PHP/Java/pureHtml 应用程序,您仍然可以使用该部分添加重写规则。重写将在您的请求到达您的应用程序之前发生。但是对于 azure 应用程序,您的意思是需要启用 ARR,它曾经有一些重写规则的问题,但现在应该没问题,因为最近我刚刚在一个新的 Azure 应用程序中为 PHP 应用程序设置了一些规则

    【讨论】:

    • 我们正在使用 web.config 中的 IIS 重写规则。
    【解决方案2】:

    您可以使用应用服务custom extension

    例如,有一个扩展程序强制流量通过 HTTPS,在此 post 中进行了描述。

    【讨论】:

      【解决方案3】:

      This 将所有 http 流量重定向到 https。它还确保站点预热请求通过,从而使站点交换和始终在线方案中的工作正常。

      <?xml version="1.0"?>
      <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-
      Transform">
      <location path="%XDT_SITENAME%" xdt:Transform="InsertIfMissing" 
      xdt:Locator="Match(path)">
      <system.webServer xdt:Transform="InsertIfMissing">
        <applicationInitialization xdt:Transform="InsertIfMissing">
          <add initializationPage="/"  xdt:Transform="InsertIfMissing"/>
        </applicationInitialization>
      
        <rewrite xdt:Transform="InsertIfMissing">
          <rules xdt:Transform="InsertIfMissing">
            <rule name="Force HTTPS" enabled="true" stopProcessing="true">
              <match url="(.*)" ignoreCase="false" />
              <conditions>
                <add input="{HTTPS}" pattern="off" />
                <add input="{WARMUP_REQUEST}" pattern="1" negate="true" />
              </conditions>
              <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" 
       appendQueryString="true" redirectType="Permanent" />
            </rule>
          </rules>
        </rewrite>    
      </system.webServer>
      

      【讨论】:

        猜你喜欢
        • 2014-04-27
        • 2016-07-27
        • 2012-04-19
        • 1970-01-01
        • 1970-01-01
        • 2016-10-28
        • 2015-03-01
        • 2014-08-23
        • 2013-01-06
        相关资源
        最近更新 更多