views:

38

answers:

3

I have read the previous posts about using the RequireHttpsAttribute to secure individual controllers:

http://stackoverflow.com/questions/1639707/asp-net-mvc-requirehttps-in-production-only

but is there a way to apply this to the entire site? Due to my host (discountasp.net) I cannot use the "RequireSSL IIS" setting.

A: 

You could use a base class for all of your controllers, and decorate that with the require ssl attribute.

DanP
I (or another team member) don't want to accidentally forget to add the attribute to a new controller, or likewise, forget to inherit from the base class. We want it to be foolproof.
CodeGrue
@CodeGrue: you could always add a unit test that asserts that all of your controllers use this attribute; see my recent question regarding the serializable attribute: http://stackoverflow.com/questions/3257004/how-to-determine-if-all-objects-are-serializable-in-a-given-namespace
DanP
A: 

You could always add a check at the application level in your global.asax

protected void Application_BeginRequest(Object sender, EventArgs e)
{
   if (HttpContext.Current.Request.IsSecureConnection == false)
   {
    Response.Redirect("https://" + Request.ServerVariables["HTTP_HOST"] + HttpContext.Current.Request.RawUrl);
   }
}
Todd Smith
A: 

I ended up using IIS URL Rewrite 2.0 to force the site to switch to HTTPS. This code in web.config does the trick:

  <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>
CodeGrue