通过SSL / TLS使用WebApi Self Host

时间:2016-04-23 09:06:21

标签: c# ssl asp.net-web-api self-hosting

我有一个基于SSL的WebApi自托管控制台服务器。

服务器负责创建SSL证书,将它们添加到证书存储区,并使用netsh将证书绑定到端口。

服务器有一个简单的控制器,它通过HTTP GET返回字符串“Hello World”。

Browser is able to access the server

我可以通过浏览器访问它而没有任何问题,我很确定服务器代码没有任何问题,所以我只会在这里发布有问题的客户端代码。

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)
                =>
                {
                    try
                    {
                        ProcessResponse(response);
                    }
                    catch (AggregateException agException)
                    {
                        throw new Exception("Error getting response: " + agException.Message);
                    }
                }).ConfigureAwait(false);
        }
    }

    private static void ProcessResponse(Task<HttpResponseMessage> response)
    {
        Console.WriteLine(response.Result.Content.ReadAsStringAsync().Result);
    }

    private static void ProcessResponseHeaders(Task<HttpResponseMessage> response)
    {
        Console.WriteLine(response.Result.Headers.ToString());
    }

private static WebRequestHandler GetSSLHandler()
    {
        WebRequestHandler handler = new WebRequestHandler();
        X509Certificate2 certificate = GetMyX509Certificate();
        handler.ClientCertificates.Add(certificate);
        return handler;
    }

现在在我的主要例程中我简单地称之为:

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

现在我的问题是,如果我尝试阅读应该给我“Hello World”的响应内容,它会给我一个空字符串。

所以我尝试查看响应标头,这就是我得到的结果:

Response headers

似乎正在经历谈判阶段,我不知道该怎么办。

请指教。提前谢谢。

修改

很抱歉问题出在服务器代码上。只需取消注释调用httpBinding.ConfigureTransportBindingElement。

的部分
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 =>
      element.AuthenticationScheme =
      AuthenticationSchemes.Negotiate);
    */
    return base.OnConfigureBinding(httpBinding);
  }
}

1 个答案:

答案 0 :(得分:1)

请尝试运行:

var client = new HttpClient
{
    BaseAddress = new Uri("https://localhost:4443/")
};
var result = await client.GetAsync("WebApi/Service/HelloWorld").ConfigureAwait(false);
var data = await result.Content.ReadAsStringAsync().ConfigureAwait(false);
return data;

作为您的请求任务。

尝试更改

ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;