Added X509Certificate2Filename (#27)

This commit is contained in:
Stef Heyenrath
2017-05-16 20:20:55 +02:00
parent 961e8b555f
commit 360a1227e4
17 changed files with 122 additions and 65 deletions

View File

@@ -7,10 +7,39 @@ namespace WireMock.Http
{
internal static class HttpClientHelper
{
private static HttpClient client = new HttpClient();
public static async Task<ResponseMessage> SendAsync(RequestMessage requestMessage, string url)
private static HttpClient CreateHttpClient(string clientX509Certificate2Filename = null)
{
if (!string.IsNullOrEmpty(clientX509Certificate2Filename))
{
#if NETSTANDARD || NET46
var handler = new HttpClientHandler
{
ClientCertificateOptions = ClientCertificateOption.Manual,
SslProtocols = System.Security.Authentication.SslProtocols.Tls12 | System.Security.Authentication.SslProtocols.Tls11 | System.Security.Authentication.SslProtocols.Tls,
ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true
};
handler.ClientCertificates.Add(new System.Security.Cryptography.X509Certificates.X509Certificate2(clientX509Certificate2Filename));
return new HttpClient(handler);
#else
var handler = new WebRequestHandler
{
ClientCertificateOptions = ClientCertificateOption.Manual,
ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true
};
handler.ClientCertificates.Add(new System.Security.Cryptography.X509Certificates.X509Certificate2(clientX509Certificate2Filename));
return new HttpClient(handler);
#endif
}
return new HttpClient();
}
public static async Task<ResponseMessage> SendAsync(RequestMessage requestMessage, string url, string clientX509Certificate2Filename = null)
{
var client = CreateHttpClient(clientX509Certificate2Filename);
var httpRequestMessage = new HttpRequestMessage(new HttpMethod(requestMessage.Method), url);
// Overwrite the host header