我正在尝试在web.config文件的部分中编写HSTS重写规则
<rewrite>
<rules>
<rule name="HTTP to HTTPS redirect" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^test\.test\.test$" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://test.test.test/{R:1}"
redirectType="Permanent" />
</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; includeSubDomains; preload" />
</rule>
</outboundRules>
</rewrite> 我可以在IIS中看到这些重定向和重写规则,但不确定为什么通过开发工具F12检查时不存在严格传输安全头,而且网站功能齐全
我知道有另一种方法可以在web.config中添加自定义头部,但通过查看SO上的几篇文章,它看起来不像是一种推荐的方法。
有人能告诉我我做错了什么吗?
发布于 2020-09-10 15:12:42
我不认为你的“HTTP到HTTPS重定向”规则是正确的。请打开失败请求跟踪,查看是否生效。因为HTTPS的值只有On或Off,所以当我测试模式时,它总是失败。更多的服务器变量可以引用this。
<rewrite>
<rules>
<rule name="HTTP to HTTPS redirect" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="Off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://test.test.test/{R:1}"
redirectType="Permanent" />
</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; includeSubDomains; preload" />
</rule>
</outboundRules>当我改变模式时,一切都进行得很顺利。可以在响应报头中找到Strict-Transport-Security报头。

微软还推荐了另外两种在headers.One响应报头模块中设置自定义报头的方式。这种方式是快速和simple.Refer到this。另一种是自定义过滤器。下面是一些示例代码。
public class CustomFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
//add in your custom headers
filterContext.HttpContext.Response.AddHeader("Access-Control-Allow-Origin", "*");
filterContext.HttpContext.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type");
filterContext.HttpContext.Response.AddHeader("Access-Control-Allow-Methods", "GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS");
base.OnActionExecuting(filterContext);
}
public void OnException(ExceptionContext filterContext)
{
//do some cool exception handling here
}
}https://stackoverflow.com/questions/63814910
复制相似问题