title: ASP.NET Web API : An existing connection was forcibly closed by the remote host
body: <p>Web Api </p>
  
  <pre><code>[HttpGet]
  [RequireHttps]
  [Route("Api/GetString")]
  public string GetString()
  {
     return "Worked";
  }
  </code></pre>
  
  <p>RequireHttpsAttribute class</p>
  
  <pre><code>public class RequireHttpsAttribute : AuthorizationFilterAttribute
  {
      public override void OnAuthorization(HttpActionContext actionContext)
      {
          if (actionContext.Request.RequestUri.Scheme != Uri.UriSchemeHttps)
          {
              actionContext.Response = new HttpResponseMessage(System.Net.HttpStatusCode.Forbidden)
              {
                  ReasonPhrase = "HTTPS Required"
              };
          }
          else
          {
              var cert = actionContext.Request.GetClientCertificate();
              if (cert == null)
              {
                  actionContext.Response = new HttpResponseMessage(System.Net.HttpStatusCode.Forbidden)
                  {
                      ReasonPhrase = "Client Certificate Required"
                  };
  
              }
              base.OnAuthorization(actionContext);
          }
      }
  }
  </code></pre>
  
  <p>Client (console)</p>
  
  <pre><code>HttpWebRequest client = (HttpWebRequest)WebRequest.Create("https://localhost:44315/api/GetString");
  var cert = new X509Certificate2(File.ReadAllBytes(@"C:\ConsoleClient\TestCertificate.pfx"), "password");
  client.ClientCertificates.Add(cert);
  HttpWebResponse resp = (HttpWebResponse)client.GetResponse();
  using (var readStream = new StreamReader(resp.GetResponseStream()))
  {
     Console.WriteLine(readStream.ReadToEnd());
  }
  </code></pre>
  
  <p>This https request throws error.</p>
  
  <pre><code>Inner Exception Message : Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
  Message : The underlying connection was closed: An unexpected error occurred on a send.
  </code></pre>
  
  <p>I opened port 443 and tried some settings mentioned in answers of same type of questions but couldn't resolve. Direct access to <a href="https://localhost:44315/api/GetString" rel="nofollow noreferrer">https://localhost:44315/api/GetString</a> via browser is also not working.</p>
  
  <p>If I change the https to http, it works fine.</p>
  
  <p>Is anything needs to be configured?</p>
  
  <p>Thanks.</p>