title: self hosted WEB API over HTTPS. Reject all HTTP calls
body: <p>I have a self hosted WEB API project that needs to run over HTTPS only. </p>
  
  <p>I found several articles how to do that (get SSL certificate, bind the certificate to the IP/Port using 'netsh' command, change base address to HTTPS://..., etc).</p>
  
  <p>All looks good so far and I can call the service over HTTPS. </p>
  
  <p>But I also need to explicitly reject all HTTP calls.  When I try to call the service using HTTP I am getting '504 Timeout" error. And it takes a while to get a response from the service, it's possible to get Denial of Service. </p>
  
  <p>I tried to apply a custom attribute that will check if the request comes over HTTPS but that code is never reached.</p>
  
  <p>What do i need to do in order to reject HTTP calls.  Again it's a self host WEB API project.</p>
  
  <p>I am not hosting WEB API in IIS. It's a self hosted WEB API. Here's the code how I initialize it:</p>
  
  <pre><code>public class HttpsSelfHostConfiguration : HttpSelfHostConfiguration
  {
     public HttpsSelfHostConfiguration(string baseAddress) : base(baseAddress) { }
     public HttpsSelfHostConfiguration(Uri baseAddress) : base(baseAddress) { }
  
    protected override BindingParameterCollection OnConfigureBinding(HttpBinding httpBinding)
    {
        if (BaseAddress.ToString().ToLower().Contains("https://"))
        {
            httpBinding.Security.Mode = HttpBindingSecurityMode.Transport;
        }
        return base.OnConfigureBinding(httpBinding);
    }
  }
  
  var url = "https://localhost:7080/";
  
  var config = new HttpsSelfHostConfiguration(url);
  
  config.Routes.MapHttpRoute("WebAPIRoute2", "api/{controller}/{action}/{id}",
        new { controller = "Submissions", id = RouteParameter.Optional });
  
  
  var server = new HttpSelfHostServer(config);
  server.OpenAsync().Wait();
  </code></pre>
  
  <p>I also  used the following commands to bind a SSL certificate to port 7080:</p>
  
  <pre><code>netsh http add urlacl url=https://+:7080/ user=Everyone
  
  netsh http add sslcert ipport=0.0.0.0:7080 certhash=Thumbprint appid={xxxx}
  </code></pre>