大约6个月前,我推出了一个网站,每个请求都需要通过https.当时我能找到的确保每个页面请求都通过https的唯一方法是在页面加载事件中判断它.如果请求不是通过http,我将响应.重定向("https://example.com")
有没有更好的方法--最好是在web.config中进行一些设置?
大约6个月前,我推出了一个网站,每个请求都需要通过https.当时我能找到的确保每个页面请求都通过https的唯一方法是在页面加载事件中判断它.如果请求不是通过http,我将响应.重定向("https://example.com")
有没有更好的方法--最好是在web.config中进行一些设置?
请使用HSTS(HTTP严格传输安全)
从http://www.hanselman.com/blog/HowToEnableHTTPStrictTransportSecurityHSTSInIIS7.aspx
<?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>
Original Answer(于2015年12月4日替换为上述内容)
基本上
protected void Application_BeginRequest(Object sender, EventArgs e)
{
if (HttpContext.Current.Request.IsSecureConnection.Equals(false) && HttpContext.Current.Request.IsLocal.Equals(false))
{
Response.Redirect("https://" + Request.ServerVariables["HTTP_HOST"]
+ HttpContext.Current.Request.RawUrl);
}
}
这将放在global.asax.cs(或global.asax.vb)中
我不知道如何在web.config中指定它