Skip to content

HttpClient class throws System.Net.InternalException when custom SSL certificate is added to the related HttpMessageHandler #28714

@krishnakanumuri

Description

@krishnakanumuri

.NET Core app throws below exception when tired to use a manual SSL certificate option. The same code works works in .NET Framework. Also the same code works with .NET Core runtime on windows server 2016 version. Not sure what's going on.

.NET Core Runtime: 2.1.5
OS Version: Windows 10 1803 (build 17134.523)

System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner xception. ---> System.Net.InternalException: Exception of type 'System.Net.InternalException' was thrown.
at System.Net.SecurityStatusAdapterPal.GetSecurityStatusPalFromInterop(SECURITY_STATUS win32SecurityStatus, Boolean attachException)
at System.Net.Security.SecureChannel.GenerateToken(Byte[] input, Int32 offset, Int32 count, Byte[]& output)
at System.Net.Security.SecureChannel.NextMessage(Byte[] incoming, Int32 offset, Int32 count)
at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.PartialFrameCallback(AsyncProtocolRequest asyncRequest)
--- End of stack trace from previous location where exception was thrown ---
at System.Net.Security.SslState.ThrowIfExceptional()
at System.Net.Security.SslState.InternalEndProcessAuthentication(LazyAsyncResult lazyResult)
at System.Net.Security.SslState.EndProcessAuthentication(IAsyncResult result)
at System.Net.Security.SslStream.EndAuthenticateAsClient(IAsyncResult asyncResult)
at System.Net.Security.SslStream.<>c.b__47_1(IAsyncResult iar)
at System.Threading.Tasks.TaskFactory1.FromAsyncCoreLogic(IAsyncResult iar, Func2 endFunction, Action1 endAction, Task1 promise, Boolean requiresSynchronization)
--- End of stack trace from previous location where exception was thrown ---
at System.Net.Http.ConnectHelper.EstablishSslConnectionAsyncCore(Stream stream, SslClientAuthenticationOptions sslOptions, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at System.Net.Http.ConnectHelper.EstablishSslConnectionAsyncCore(Stream stream, SslClientAuthenticationOptions sslOptions, CancellationToken cancellationToken)
at System.Threading.Tasks.ValueTask1.get_Result() at System.Net.Http.HttpConnectionPool.CreateConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken) at System.Threading.Tasks.ValueTask1.get_Result()
at System.Net.Http.HttpConnectionPool.WaitForCreatedConnectionAsync(ValueTask1 creationTask) at System.Threading.Tasks.ValueTask1.get_Result()
at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
at HTTPClientCore.Program.Main(String[] args) in C:\Krishna\WS\HTTPClientCore\Program.cs:line 33

Here is the code of my app

ServicePointManager.Expect100Continue = true;          
HttpClientHandler h = new HttpClientHandler();
h.ClientCertificateOptions = ClientCertificateOption.Manual;
Console.WriteLine("Enter Certificate Path:");
string file = Console.ReadLine();
int res = h.ClientCertificates.Add(new X509Certificate2(file, "fred"));                
h.ServerCertificateCustomValidationCallback = (a,b,c,d)=> { return true; };
HttpClient client = new HttpClient(h);               
Console.WriteLine("Enter request path:");
string request = Console.ReadLine();
StringContent body = new StringContent(File.ReadAllText(request));
body.Headers.ContentType = new MediaTypeHeaderValue("text/xml");
body.Headers.Add("SOAPAction", "getPoleObjects");
var responseMessage = await client.PostAsync("https://xx.xx.xx.xxx/servicename", body);
string result = await responseMessage.Content.ReadAsStringAsync();
Console.WriteLine(result);

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions