这个问题可能被问了一千遍,但我仍然找不到解决方案..
我有一个 .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 月为止都是最新的。
- 转到 Azure 门户中的应用服务。
- 点击顶部菜单中的“工具”。
- 在“开发”部分中,点击“扩展”。
- 在“扩展程序”边栏选项卡中,点击顶部的“添加”。
- 在“选择扩展”边栏选项卡中,选择“将 HTTP 重定向到 HTTPS”。
- 单击“确定”或其他任何按钮,直至保存。
之后,您的所有 HTTP 流量都将重定向到 HTTPS,无需进行任何其他更改。
关于.net - HTTPS 重定向在本地有效,但在 azure 上无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38915505/