title: Redirect loop with SSL action filter in ASP.NET MVC
body: <p>I am using an ActionFilter (see below) to detect whether or not 1. the current controller/action requires SSL and 2. SSL is currently being used, and redirect accordingly.</p>
  
  <p>This works fine locally (using a dummy cert in IIS 7) but once I get it up on the server I get an error indicating an infinite redirect loop.</p>
  
  <p>Any ideas?</p>
  
  <pre><code> public class SslFilter : ActionFilterAttribute
  {
      public SslFilter(bool sslRequired)
      {
          SslRequired = sslRequired;
      }
  
      public bool SslRequired { get; set; }
  
  
      public override void OnActionExecuting(ActionExecutingContext filterContext)
      {
          HttpRequestBase req = filterContext.HttpContext.Request;
          HttpResponseBase res = filterContext.HttpContext.Response;
  
          var builder = new UriBuilder(req.Url);
  
          if (SslRequired &amp;&amp; !req.IsSecureConnection)
          {
              builder.Scheme = Uri.UriSchemeHttps;
              builder.Port = 443;
              res.Redirect(builder.Uri.ToString());
  
          }
          else if (!SslRequired &amp;&amp; req.IsSecureConnection)
          {
              builder.Scheme = Uri.UriSchemeHttp;
              builder.Port = 80;
              res.Redirect(builder.Uri.ToString());
          }
  
          base.OnActionExecuting(filterContext);
      }
  }
  </code></pre>
  
  <p>Firefox error:</p>
  
  <blockquote>
    <p>The page isn't redirecting properly</p>
    
    <p>Firefox has detected that the server
    is redirecting the request for this
    address in a way that will never
    complete.</p>
    
    <p>This problem can sometimes be caused
    by disabling or refusing to accept
    cookies.</p>
  </blockquote>