title: ASP.NET MVC: How to automatically disable [RequireHttps] on localhost?
body: <p>I want my login page to be SSL only:</p>
  
  <pre><code>    [RequireHttps]
      public ActionResult Login()
      {
          if (Helper.LoggedIn)
          {
              Response.Redirect("/account/stats");
          }
  
          return View();
      }
  </code></pre>
  
  <p>But obviously it doesn't work on localhost when I develop and debug my application. I don't wanna use IIS 7 with SSL certificates, how can I automatically disable the RequireHttps attribute?</p>
  
  <p><strong>Update</strong></p>
  
  <p>Based on info provided by StackOverflow users and ASP.NET MVC 2 source code I created the following class that solves the problem.</p>
  
  <pre><code>public class RequireSSLAttribute : FilterAttribute, IAuthorizationFilter
  {
      public virtual void OnAuthorization(AuthorizationContext filterContext)
      {
          if (filterContext == null)
          {
              throw new ArgumentNullException("filterContext");
          }
  
          if (!filterContext.HttpContext.Request.IsSecureConnection)
          {
              HandleNonHttpsRequest(filterContext);
          }
      }
  
      protected virtual void HandleNonHttpsRequest(AuthorizationContext filterContext)
      {
          if (filterContext.HttpContext.Request.Url.Host.Contains("localhost")) return;
  
          if (!String.Equals(filterContext.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
          {
              throw new InvalidOperationException("The requested resource can only be accessed via SSL");
          }
  
          string url = "https://" + filterContext.HttpContext.Request.Url.Host + filterContext.HttpContext.Request.RawUrl;
          filterContext.Result = new RedirectResult(url);
      }
  }
  </code></pre>
  
  <p>And it's used like this:</p>
  
  <pre><code>[RequireSSL]
  public ActionResult Login()
  {
      if (Helper.LoggedIn)
      {
          Response.Redirect("/account/stats");
      }
  
      return View();
  }
  </code></pre>