Asp.net
此站點無法提供安全連接
當我在 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>但是當我在本地機器上執行相同的程式碼時,會出現以下錯誤。
此站點無法提供安全連接
當我在本地機器上執行上述程式碼時,如何解決上述錯誤?
我個人所做的就是將重寫配置放入 Web.Release.config 正是因為讓它在本地工作有點繁瑣。
問題是 IIS Express 會在不同的埠上公開 HTTP 和 HTTPS,所以如果你從 重定向
http://localhost:1234到https://localhost:1234,它根本就行不通,因為 IIS Express 在類似https://localhost:44300.您可以在 IIS Express 上啟用 SSL/TLS(並且您應該),但我會將重寫規則僅用於發布模式。
這是一個範例 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 中,否則我將插入它。
