Consuming WebApi Self Host over SSL/TLS
<p>I have a WebApi self hosted console server over SSL.</p>

<p>The server takes care of creating the SSL certificates, adding them to the certificate store and binds the certificate to the port using netsh all on the fly.</p>

<p>The server has a simple controller that returns the string "Hello World" through HTTP GET.</p>

<p><a href="https://i.stack.imgur.com/xAcWD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xAcWD.png" alt="Browser is able to access the server"></a></p>

<p>I can access it through the browser without any problems and I am quite certain there is nothing wrong with the server code so I am only going to post the troubled client code here.</p>

<pre><code>private static string url = @"https://localhost:4443/WebApi/Service/HelloWorld;

private static async Task GetHelloWorldRequest(string url)
    {
        using (HttpClient httpClient = new HttpClient(GetSSLHandler()))
        {
            HttpRequestMessage request = new HttpRequestMessage();
            request.Method = HttpMethod.Get;
            request.RequestUri = new Uri(url);

            await httpClient
                .SendAsync(request)
                .ContinueWith((response)
                =&gt;
                {
                    try
                    {
                        ProcessResponse(response);
                    }
                    catch (AggregateException agException)
                    {
                        throw new Exception("Error getting response: " + agException.Message);
                    }
                }).ConfigureAwait(false);
        }
    }

    private static void ProcessResponse(Task&lt;HttpResponseMessage&gt; response)
    {
        Console.WriteLine(response.Result.Content.ReadAsStringAsync().Result);
    }

    private static void ProcessResponseHeaders(Task&lt;HttpResponseMessage&gt; response)
    {
        Console.WriteLine(response.Result.Headers.ToString());
    }

private static WebRequestHandler GetSSLHandler()
    {
        WebRequestHandler handler = new WebRequestHandler();
        X509Certificate2 certificate = GetMyX509Certificate();
        handler.ClientCertificates.Add(certificate);
        return handler;
    }
</code></pre>

<p>Now in my main routine I simply call this:</p>

<pre><code>Console.WriteLine("Response headers:");
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
Task task = GetHelloWorldRequest(url);
task.Wait();
</code></pre>

<p>Now my problem is if I try to read the response content which should give me "Hello World" it gives me an empty string instead.</p>

<p>So I tried looking at the response headers and this is what I get:</p>

<p><a href="https://i.stack.imgur.com/e8bQT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/e8bQT.png" alt="Response headers"></a></p>

<p>It seems to be it is going through the negotiation phase and I don't know what to do from here.</p>

<p>Please advice. Thank you in advance.</p>

<p><strong>EDIT:</strong></p>

<p>Sorry the problem is with the server code. Simply uncomment the section calling httpBinding.ConfigureTransportBindingElement.</p>

<pre><code>class SslHttpsSelfHostConfiguration : HttpSelfHostConfiguration
{
  public SslHttpsSelfHostConfiguration(string baseAddress) : base(baseAddress) { }
  public SslHttpsSelfHostConfiguration(Uri baseAddress) : base(baseAddress) { }

  protected override BindingParameterCollection OnConfigureBinding(HttpBinding httpBinding)
  {
    httpBinding.Security.Mode = HttpBindingSecurityMode.Transport;
    /*
    httpBinding.ConfigureTransportBindingElement = (element =&gt;
      element.AuthenticationScheme =
      AuthenticationSchemes.Negotiate);
    */
    return base.OnConfigureBinding(httpBinding);
  }
}
</code></pre>