title: IHTTPModule to switch between HTTP and HTTPS in ASP.NET
body: <p>I'm working on a web site which contains sections that need to be secured by SSL. </p>
  
  <p>I have the site configured so that it runs fine when it's always in SSL, I see the SSL padlock in IE7/IE8/FireFox/Safari/Chrome</p>
  
  <p>To implement the SSL switching, I created a class that implemented IHTTPModule and wired up HTTPApplication.PreRequestHandlerExecute.</p>
  
  <p>I go through some custom logic to determine whether or not my request should use SSL, and then I redirect. I have to deal with two scenarios:</p>
  
  <ul>
  <li>Currently in SSL and request doesn't require SSL</li>
  <li>Currently not in SSL but request requires SSL</li>
  </ul>
  
  <p>I end up doing the followng (where ctx is HttpContext.Current and pathAndQuery is ctx.Request.Url.PathAndQuery)</p>
  
  <pre><code>// SSL required and current connection is not SSL
  if (requestRequiresSSL &amp; !ctx.Request.IsSecureConnection)
     ctx.Response.Redirect("https://www.myurl.com" + pathAndQuery);
  // SSL not required but current connection is SSL
  if (!requestRequiresSSL &amp; ctx.Request.IsSecureConnection)
     ctx.Response.Redirect("http://www.myurl.com" + pathAndQuery);
  </code></pre>
  
  <p>The switching back and forth now works fine. However, when I go into SSL mode, FireFox and IE8 warns me that my request isn't entirely encrypted. </p>
  
  <p>It looks like my module is short circuiting my request somehow, would appreciate any thoughts. </p>