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

C#Web ServicesWindows AuthenticationHttpwebresponse

C# Problem Overview


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

C# Solutions


Solution 1 - C#

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;

Solution 2 - C#

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

Solution 3 - C#

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!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChirag KView Question on Stackoverflow
Solution 1 - C#Chirag KView Answer on Stackoverflow
Solution 2 - C#Thong NguyenView Answer on Stackoverflow
Solution 3 - C#jitin14View Answer on Stackoverflow