Search code examples
c#restwcfsslservice

The request was aborted: Could not create SSL/TLS secure channel c# Webrequest


I am trying to call a service by using below set of code in c#. i'am using the .NET Framework 4.6.2. and whenever i reach the code (HttpWebResponse)myHttpWebRequest.GetResponse(), it is throwing an exception "The request was aborted: Could not create SSL/TLS secure channel". I tried many solutions said in stack overflow. but none of them were not helpful for me. i am struggling on this from last one week then finally posting here. please any help will be appreciated. I even added the certificated in local stores but still no use. below is my code.

ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

Uri myUri = new Uri("https://CallingUrl");
WebRequest myWebRequest = HttpWebRequest.Create(myUri);
HttpWebRequest myHttpWebRequest = (HttpWebRequest)myWebRequest;
string svcCredentials = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes("User:Pwd"));
myHttpWebRequest.Headers.Add("Authorization", "Basic " + svcCredentials);
myHttpWebRequest.PreAuthenticate = true;
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
myHttpWebRequest.Method = "POST"
HttpWebResponse httpResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();

Solution

  • The above code is perfectly fine. but this issue was caused by other reason.

    After digging into a lot finally go the solution, and thought it will help others who are facing same issue so posting this answer.

    Cause :

    By default limit in Schannel implementation is 32768 bytes. Once it reached the maximum limit it will return above error at the time of handshake between Service.

    Resolution :

    1. Increase the MessageLimitClient from Default 0x8000 to max 0xf000 with the registry key HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\SecurityProviders\Schannel\Messaging

    2. Create the Messaging Key if it is not present

    3. Then Create New DWORD name it as 'MessageLimitClient' and set the Valuedata as 'f000' (hexadecimal).

    4. After changing this value, restart server to take effect of new value.

    enter image description here