57

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

Nisarg Shah
  • 14,151
  • 6
  • 34
  • 55
Chirag K
  • 2,394
  • 2
  • 16
  • 23
  • 1
    http://stackoverflow.com/questions/19639846/failure-on-httpwebrequest-with-inner-exception-authentication-failed-because-the – Amit Kumar Ghosh Feb 25 '16 at 08:22
  • I tried that, It does not solve the problem. – Chirag K Feb 25 '16 at 09:27
  • 1
    Possible duplicate of [Authentication failed because remote party has closed the transport stream](https://stackoverflow.com/questions/30664566/authentication-failed-because-remote-party-has-closed-the-transport-stream) – Nisarg Shah Aug 08 '18 at 13:22

3 Answers3

143

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;
Chirag K
  • 2,394
  • 2
  • 16
  • 23
  • 5
    You can add it before getting the HTTPResponse from request. – Chirag K Dec 30 '16 at 09:24
  • My SecurityProtocolType doesn't show option for Tls12 and Tls11. I am using .net framework 4. – Arti Berde Feb 16 '17 at 07:31
  • 1
    .Net 4.0 does not know about Tls11 and Tls12. To access those you need to be using .Net 4.5 or later. – Jinlye Aug 29 '17 at 14:40
  • 13
    If using .Net 4.0 you can use a numeric value for TLS1.2 `ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;` – Young Bob Nov 07 '17 at 18:55
  • 2
    Also see https://blogs.perficient.com/microsoft/2016/04/tsl-1-2-and-net-support/ for other options and frameworks. – Young Bob Nov 07 '17 at 19:02
  • 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.2 – hiFI Aug 19 '19 at 05:58
  • 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. – ArXen42 Mar 02 '20 at 12:42
  • 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. – Oliver Clancy Apr 09 '20 at 11:11
  • 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" – jmiguel77 Jun 23 '20 at 19:41
  • any comments on what if I get this error while trying to generate code using the CMD? – Zeeshan Adil May 29 '21 at 10:34
  • The Best answer for all solutions is in this link https://www.py4u.net/discuss/711097 – keivan kashani Sep 02 '21 at 07:42
  • I used this solution but using ServicePointManager.SecurityProtocol before making request and this does not work when using before response – keivan kashani Sep 02 '21 at 07:45
  • 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 stream – that_noob May 22 '23 at 12:32
1

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);
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!

jitin14
  • 144
  • 5
  • any comments on what if I get this error while trying to generate code using the CMD? – Zeeshan Adil May 29 '21 at 10:34
  • 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 – jitin14 Jun 22 '21 at 05:32