Pra*_*eep 10 asp.net security web-config url-rewriting azure
当我在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>
Run Code Online (Sandbox Code Playgroud)
但是,当我在本地计算机上运行相同的代码时,会出现以下错误.
此站点无法提供安全连接
在本地计算机上运行上述代码时,如何解决上述错误?
小智 15
这总是为我解决问题。
我个人所做的就是将重写配置精确地放入Web.Release.config中,因为让它在本地运行有点繁琐.
问题是IIS Express会在不同的端口上公开HTTP和HTTPS,所以如果你重定向http://localhost:1234到https://localhost:1234它,它就行不通,因为IIS Express会在类似的东西上暴露HTTPS 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>
Run Code Online (Sandbox Code Playgroud)
请注意,我还在这里添加了HSTS.它<rewrite>在发布模式下将元素插入到Web.config中.该<system.webServer>元素已存在于Web.config中,否则我将插入该元素.
| 归档时间: |
|
| 查看次数: |
21586 次 |
| 最近记录: |