在IIS 7中启用HTTP严格传输安全性(HSTS)

在IIS 7 Web服务器上打开HTTP严格传输安全性的最佳方法是什么?

我可以通过GUI并添加正确的HTTP响应头或我应该使用appcmd,如果是的话,什么开关?

    IIS 能够将自定义标题添加到响应中 。 这似乎是最简单的方法去做。

    根据IIS.net上的文档,您可以通过IISpipe理器添加这些标头:

    • 在“连接”窗格中,转至要为其设置自定义HTTP标头的站点,应用程序或目录。
    • 在“主页”窗格中,双击“HTTP响应标题”。
    • 在“HTTP响应标题”窗格中,单击“操作”窗格中的“添加…”。
    • 在“添加自定义HTTP响应标题”对话框中,设置自定义标题的名称和值,然后单击“确定”。

    这样,我们就可以同时处理HTTPredirect,并将Strict-Transport-Security头添加到HTTPS响应中(使用单个IIS站点(需要安装URL重写模块)):

    <?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <rewrite> <rules> <rule name="HTTP to HTTPS redirect" 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> <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> </configuration> 

    为了补充voretaq7的答案,您也可以使用Web.config文件(注意:仅用于SSL站点,因为它将为HTTP和HTTPS响应添加标头,这违反了RFC 6797规范,请看下面的解释) – 添加一个块如下:

     <system.webServer> <httpProtocol> <customHeaders> <add name="Strict-Transport-Security" value="max-age=31536000"/> </customHeaders> </httpProtocol> </system.webServer> 

    很明显,你的Web.config中可能已经有了一个system.webServer块,所以把它添加到那里。 我们更喜欢在Web.config而不是GUI中处理事情,因为这意味着configuration更改可以提交到我们的Git存储库。

    如果您想要处理HTTP到SSL的redirect,就像Greg Askew所说的那样,您可能会发现使用IIS中的单独网站更容易。 这就是我们如何处理为某些客户站点要求SSL。 该网站只包含一个HTTPredirect和一些信息披露修正,全部在Web.config中:

     <?xml version="1.0" encoding="UTF-8"?> <configuration> <system.web> <httpRuntime requestValidationMode="2.0" enableVersionHeader="false" /> </system.web> <system.webServer> <httpRedirect enabled="true" destination="https://www.domain.co.uk/" httpResponseStatus="Permanent" /> <httpProtocol> <customHeaders> <remove name="X-Powered-By" /> </customHeaders> </httpProtocol> <rewrite> <outboundRules> <rule name="Remove RESPONSE_Server"> <match serverVariable="RESPONSE_Server" pattern=".+" /> <action type="Rewrite" value="" /> </rule> </outboundRules> </rewrite> </system.webServer> </configuration> 

    这是我们首选的解决scheme,我们可以很容易地loggingredirect的stream量(因为它在不同的IIS日志中),它不涉及更多的代码在Global.asax.cs中(我们没有任何代码在那里,这对Umbraco站点来说更方便一些),重要的是,这意味着所有的configuration仍然保存在我们的GIT仓库中。

    编辑添加:要清楚,为了符合RFC 6797 , Strict-Transport-Security自定义标头不能被添加到未encryption的HTTP所做的请求中。 要符合RFC6797,你必须在IIS中有两个站点,正如我在第一个代码块之后所描述的那样。 Chris指出,RFC 6797包括:

    HSTS主机不得在通过非安全传输传送的HTTP响应中包含STS头域。

    所以发送Strict-Transport-Security客户头部以响应非SSL请求将不符合该规范。

    我将使用您引用的Wikipedia链接中的示例,并在该站点的global.asax中执行活动。 这可以将请求redirect到https url, 然后将该头插入到响应中。

    这是由于如果HSTS头不在https响应中,它必须被忽略。

     protected void Application_BeginRequest() { switch (Request.Url.Scheme) { case "https": Response.AddHeader("Strict-Transport-Security", "max-age=31536000"); break; case "http": var path = "https://" + Request.Url.Host + Request.Url.PathAndQuery; Response.Status = "301 Moved Permanently"; Response.AddHeader("Location", path); break; } } 

    这似乎是一个相当安全的方式来做到这一点。 在Global.asax中添加此代码 – Application_BeginRequest事件首先在Asp.net请求生命周期中触发: http : //msdn.microsoft.com/zh-cn/library/system.web.httpapplication.beginrequest( v = vs。 110)的.aspx

    根据规范,http请求不能用头来响应 – 所以这个代码只会把它添加到https请求中。 最大年龄以秒为单位,在这里input一个大的值通常是一个好主意(IE – 31536000表示该网站将仅在未来365天内运行SSL)

     protected void Application_BeginRequest(Object sender, EventArgs e) { switch (Request.Url.Scheme) { case "https": Response.AddHeader("Strict-Transport-Security", "max-age=31536000"); break; case "http": var path = "https://" + Request.Url.Host + Request.Url.PathAndQuery; Response.Status = "301 Moved Permanently"; Response.AddHeader("Location", path); break; } } 

    根据HTTP严格传输安全IIS模块的制造商,只是添加自定义标头不符合草案规范(RFC 6797)。

    您实际上需要安装此IIS模块以启用IIS 7上的HSTS。

    更新2014年10月26日 :感谢下面的评论者,我再次阅读模块页面,特别是通过添加自定义标题来certificate使用模块的部分。

    HSTS主机不得在通过非安全传输传送的HTTP响应中包含STS头字段。

    如果您确保只在HTTPS中添加标题,而不在HTTP中,则不需要此模块,您可以使用Doug Wilson的答案。 不要使用Owen Blacker的答案,因为它没有https条件。

    使用Doug Wilson提供的示例,我创build了以下两个PowerShell函数,将redirect的URL重写规则添加到HTTPS以及添加HSTS标头。

    这些已经在Windows 2012和Windows 2012 R2上进行了testing。

    所有你需要做的是提供网站的名称。 如果您不喜欢默认值,您可以select给规则一个不同的名字。

    有一点需要注意的是,从我的testing中,服务器variables需要被添加到允许列表中,然后才能进入响应头。 这些function为你做。

    编辑:请参阅Url重写HTTP头在这里参考: http : //www.iis.net/learn/extensions/url-rewrite-module/setting-http-request-headers-and-iis-server-variables

     Function Add-HTTPSRedirectRewriteRule() { <# .SYNOPSIS This function is used to create a URL Rewrite Rule that redirects HTTP requests to HTTPS using a 301 RuleName is optional and will default to "Redirect to HTTPS" .SYNTAX Add-HTTPSRedirectRewriteRule -WebsiteName "www.mywebsite.com" .EXAMPLES Add-HTTPSRedirectRewriteRule -WebsiteName "www.mywebsite.com" Add-HTTPSRedirectRewriteRule -WebsiteName "www.mywebsite.com" -RuleName "my rule name" #> [cmdletbinding(positionalbinding=$false)] Param ( [parameter(mandatory=$true)][String] [ValidateNotNullOrEmpty()] $WebsiteName, [parameter(mandatory=$false)][String] $RuleName="Redirect to HTTPS" ) Write-Verbose -Message "Creating the Url Rewrite rule ""$RuleName"" in website ""$WebsiteName""" Remove-WebConfigurationProperty -pspath "MACHINE/WEBROOT/APPHOST" -location "$WebsiteName" -filter "system.webServer/rewrite/rules" -name "." -AtElement @{name="$RuleName"} -ErrorAction SilentlyContinue Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -location "$WebsiteName" -filter "system.webServer/rewrite/rules" -name "." -value @{name="$RuleName";stopProcessing='True'} Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -location "$WebsiteName" -filter "system.webServer/rewrite/rules/rule[@name='$RuleName']/match" -name "url" -value "(.*)" Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -location "$WebsiteName" -filter "system.webServer/rewrite/rules/rule[@name='$RuleName']/conditions" -name "." -value @{input='{HTTPS}';pattern='off'} Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -location "$WebsiteName" -filter "system.webServer/rewrite/rules/rule[@name='$RuleName']/action" -name "type" -value "Redirect" Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -location "$WebsiteName" -filter "system.webServer/rewrite/rules/rule[@name='$RuleName']/action" -name "url" -value "https://{HTTP_HOST}/{R:1}" } Function Add-HSTSHeaderRewriteRule() { <# .SYNOPSIS This function is used to create a URL Rewrite Rule that sets an HTTP Response Header for Strict-Transport-Security when the protocol requested is HTTPS RuleName is optional and will default to "Add Strict-Transport-Security header when request is HTTPS" .SYNTAX Add-HSTSHeaderRewriteRule -WebsiteName "www.mywebsite.com" .EXAMPLES Add-HSTSHeaderRewriteRule -WebsiteName "www.mywebsite.com" Add-HSTSHeaderRewriteRule -WebsiteName "www.mywebsite.com" -RuleName "my rule name" #> [cmdletbinding(positionalbinding=$false)] Param ( [parameter(mandatory=$true)][String] [ValidateNotNullOrEmpty()] $WebsiteName, [parameter(mandatory=$false)][String]$RuleName="Add Strict-Transport-Security header when request is HTTPS" ) $serverVariable = "RESPONSE_Strict_Transport_Security" Write-Verbose -Message "Creating the HSTS Header rule ""$RuleName"" in website ""$WebsiteName""" Remove-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -location "$WebsiteName" -filter "system.webServer/rewrite/allowedServerVariables" -name "." -AtElement @{name="$serverVariable"} -ErrorAction SilentlyContinue Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -location "$WebsiteName" -filter "system.webServer/rewrite/allowedServerVariables" -name "." -value @{name="$serverVariable"} Remove-WebConfigurationProperty -pspath "MACHINE/WEBROOT/APPHOST" -location "$WebsiteName" -name "." -filter "system.webServer/rewrite/outboundRules" -AtElement @{name="$RuleName"} -ErrorAction SilentlyContinue Add-WebConfigurationProperty -pspath "MACHINE/WEBROOT/APPHOST" -location "$WebsiteName" -filter "system.webServer/rewrite/outboundRules" -name "." -value @{name="$RuleName"} Set-WebConfigurationProperty -pspath "MACHINE/WEBROOT/APPHOST" -location "$WebsiteName" -filter "system.webServer/rewrite/outboundRules/rule[@name='$RuleName']/match" -name "serverVariable" -value $serverVariable Set-WebConfigurationProperty -pspath "MACHINE/WEBROOT/APPHOST" -location "$WebsiteName" -filter "system.webServer/rewrite/outboundRules/rule[@name='$RuleName']/match" -name "pattern" -value ".*" Add-WebConfigurationProperty -pspath "MACHINE/WEBROOT/APPHOST" -location "$WebsiteName" -filter "system.webServer/rewrite/outboundRules/rule[@name='$RuleName']/conditions" -name "." -value @{input='{HTTPS}';pattern='on'} Set-WebConfigurationProperty -pspath "MACHINE/WEBROOT/APPHOST" -location "$WebsiteName" -filter "system.webServer/rewrite/outboundRules/rule[@name='$RuleName']/action" -name "type" -value "Rewrite" Set-WebConfigurationProperty -pspath "MACHINE/WEBROOT/APPHOST" -location "$WebsiteName" -filter "system.webServer/rewrite/outboundRules/rule[@name='$RuleName']/action" -name "value" -value "max-age=31536000" } 

    这可以通过在Web.Config中添加以下块来完成:

    我们必须在IIS上进行configuration,使其能够自定义标题来响应:

    • 转到Internet信息服务(IIS)pipe理器。
    • configuration添加到服务器响应中的响应头。
    • 现在添加自定义标题名称和自定义值(自定义标题名称和值应该与Web.Config中的相同)。 你可以在博客上find