Azure Web App - 将所有流量从http重定向到https

时间:2017-09-22 16:48:55

标签: asp.net azure redirect https

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

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

我目前的理解是:

  1. 我无法在web.config中指定IIS重写规则作为应用程序 是一个Asp.Net核心应用程序
  2. 我不能用 Microsoft.AspNetCore.Rewrite中间件版本2.0.0.0版本 需要.Net Standard 2.0,而net452仅支持.Net Standard 1.5。
  3. 如果以上是正确的,那么最好的方法是什么?

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

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

您可以使用App Service custom extension

例如,有一个扩展程序会强制流量通过HTTPS传输,如post所述。

答案 2 :(得分:0)

This将所有http流量重定向到https。它还确保站点预热请求得以通过,这使得在站点交换和Always On场景中工作正常。

<?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>