我正在尝试在我的站点上强制使用HTTPS,该站点是使用AWS上的Elastic Beanstalk托管的。因为它在AWS上并且AWS Loadbalancer在IIS上插入了自己的重定向规则,所以我无法使用全局过滤器来强制HTTPS。如果我这样做,我会收到有关重写次数过多的错误。
相反,我必须使用UrlRewrite并从我的web.config传递规则我使用了从SO上的另一个答案得到的规则。这与强制HTTPS的大多数答案非常相似
<rule name="HTTP to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_X_FORWARDED_PROTO}" pattern="https" negate="true" />
<add input="{HTTPS}" pattern="^OFF$" ignoreCase="true"/>
<add input="{REMOTE_HOST}" pattern="localhost" negate="true" />
<add input="{REMOTE_ADDR}" pattern="127.0.0.1" negate="true" />
<add input="{HTTP_HOST}" pattern="localhost" negate="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
此规则适用于Safari&amp; Chrome,但是在用户首次访问该网站的IE中,它是HTTP,直到他们点击链接然后下一页是HTTPS。这种行为并不是太糟糕,但我的索引上有一个表单,当您尝试在HTTP中提交表单时,它会刷新页面并变为HTTPS。表格提交在此之后起作用。
提前感谢您提供任何帮助
答案 0 :(得分:-2)
如果您使用的是asp.net mvc,则可以在应用程序类中使用此代码而不是配置
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
//Enforce Https to the web app.
GlobalFilters.Filters.Add(new RequireHttpsAttribute());
}
}