Authentication failed because the remote party has closed the transport stream exception when getting a response from webservice
Asked Answered
N

3

59

I am calling a third party service and when I ask for a response it throws out an exception that says

"Authentication failed because the remote party has closed the transport stream exception".

I think that there is a problem in sending credentials. I have even tried supplying new credentials. Here is the full code

string get_url = "https://**.*******.com/com/******/webservices/public_webservice.cfc?wsdl&Method=CreateUser&SiteID=**&WSPassword=******&UserName=******";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(get_url);
request.MaximumAutomaticRedirections = 4;
request.MaximumResponseHeadersLength = 4;
request.Credentials = CredentialCache.DefaultCredentials;
//request.UseDefaultCredentials = false;
//request.Credentials = new System.Net.NetworkCredential("*****", "*****");
request.ContentType = "application/x-www-form-urlencoded; charset=ISO-8859-1";

// Show the sent stream
//lbl_send_stream.Text = send_stream;
//lbl_send_stream.Text = get_url;
// Get UserId And LoginToken From Third Party DB
// ==============================================
//Exception gets throwed When code hits here
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

enter image description here

Necessarian answered 25/2, 2016 at 8:12 Comment(3)
#19640346Amaro
I tried that, It does not solve the problem.Necessarian
Possible duplicate of Authentication failed because remote party has closed the transport streamParks
N
145

I found the answer, It was because the third party webservice we were calling did not support TLS 1.0 they supported 1.1 and 1.2. So I had to change the security protocol.

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
Necessarian answered 27/2, 2016 at 8:10 Comment(12)
You can add it before getting the HTTPResponse from request.Necessarian
My SecurityProtocolType doesn't show option for Tls12 and Tls11. I am using .net framework 4.Whitt
.Net 4.0 does not know about Tls11 and Tls12. To access those you need to be using .Net 4.5 or later.Glanti
If using .Net 4.0 you can use a numeric value for TLS1.2 ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;Upwards
Also see blogs.perficient.com/microsoft/2016/04/tsl-1-2-and-net-support for other options and frameworks.Upwards
My app's Target Framework was set to .Net Framework 4.5.2 but still I had to place this code to get connected to an Azure API set HTTPS only, TLS1.2Fetor
Interesting. I have WPF app on .NET 4.7.2 that started failling recently without this line. However, when executing same code from console app (also .NET 4.7.2) - no problem. Anyway, thanks for mentioning this.Word
This answer resolved my issue when I encountered the same exception when calling Azure AD secured Function Apps and Web Apis, from a desktop/native app even though the desktop app was created from the quickstart provided by the Azure portal.Swaim
I am using something similar with a FtpWebRequest; if i include a line like this: ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls13; i get this error "Authentication failed because the remote party has closed the transport stream." If i don't include that line i get this error instead: "error:1425F102:SSL routines:ssl_choose_client_version:unsupported protocol"Retrogression
any comments on what if I get this error while trying to generate code using the CMD?Indole
I used this solution but using ServicePointManager.SecurityProtocol before making request and this does not work when using before responseAbove
Tried @ChiragK but it didn't help. Still getting the same error. System.Net.WebException: 'The underlying connection was closed: An unexpected error occured in send' InnerException: IOException: Authentication failed because the remote party has closed the transport streamArticulate
M
2

I have the same issue, initially I changed the security protocol but it didn't work, then I realize that I need to change security protocol before creating the WebRequest:

  ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;
        WebRequest request = WebRequest.Create(fullURL);
        request.Headers = requestHeaders;
        byte[] Bytes = System.Text.Encoding.ASCII.GetBytes(jsonData);
Mossy answered 20/9, 2021 at 7:2 Comment(0)
A
0

I had a limitation of using TLS 1.2 only and more than anything else the resource address (URL/address) was local. So above solution didn't work for me. After closely analysing web.config I tried using <bypasslist> for local URLs and miracle happened!

<system.net>
    <defaultProxy>
      <proxy usesystemdefault="false" proxyaddress="<yourproxyaddress>" bypassonlocal="true" />
      <bypasslist>  
        <add address="[a-z]+\.abcd\.net\.au$" />  
      </bypasslist>
    </defaultProxy>
</system.net>

Please note I was already using <proxy> setting for accessing other external URLs so not having this setup was not allowed either. Hope this helps!

Antibiosis answered 26/11, 2019 at 4:18 Comment(2)
any comments on what if I get this error while trying to generate code using the CMD?Indole
Try this: add-type @" using System.Net; using System.Security.Cryptography.X509Certificates; public class TrustAllCertsPolicy : ICertificatePolicy { public bool CheckValidationResult( ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem) { return true; } } "@ [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 <your command>Antibiosis

© 2022 - 2024 — McMap. All rights reserved.