Я прочитал предыдущие сообщения об использовании RequireHttpsAttribute для обеспечения отдельных контроллеров:
ASP.NET MVC RequireHttps в производстве только
но есть ли способ применить это ко всему сайту? Из-за моего хоста (discountasp.net) я не могу использовать установку "RequireSSL IIS".
В итоге я использовал IIS URL Rewrite 2.0, чтобы заставить сайт перейти на HTTPS. Этот код в web.config делает трюк:
<system.webServer>
<!-- This uses URL Rewrite 2.0 to force the entire site into SSL mode -->
<rewrite xdt:Transform="Insert">
<rules>
<rule name="Force HTTPS" enabled="true">
<match url="(.*)" ignoreCase="false" />
<conditions>
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
Вы всегда можете добавить проверку на уровне приложения в свой global.asax
protected void Application_BeginRequest(Object sender, EventArgs e)
{
if (!HttpContext.Current.Request.IsSecureConnection)
{
Response.Redirect("https://" + Request.ServerVariables["HTTP_HOST"]
+ HttpContext.Current.Request.RawUrl);
}
}
Вы можете использовать базовый класс для всех своих контроллеров и украсить его атрибутом require ssl.