The underlying connection was closed: Could not establish secure channel for SSL/TLS

C

C.W.

I am trying to connect to a remote server in order to perform an http post
inside my asp.net page. However, I consistently get the following error:

[SocketException (0x2746): An existing connection was forcibly closed by the
remote host]
System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32
size, SocketFlags socketFlags)
System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32
size)

[IOException: Unable to read data from the transport connection.]
System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32
size)
System.Net.TlsStream.ForceRead(Byte[] space, Int32 offset, Int32 amount)
System.Net.TlsStream.ReadFullRecord(Byte[] buffer, Int32 length)
System.Net.TlsStream.Handshake(ProtocolToken message)

[WebException: The underlying connection was closed: Could not establish
secure channel for SSL/TLS.]
System.Net.HttpWebRequest.CheckFinalStatus()
System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult)
System.Net.HttpWebRequest.GetRequestStream()
......


The offending line is Dim reqWriter As StreamWriter = New
StreamWriter(req.GetRequestStream())
The code snippet is as follows:

Dim req As HttpWebRequest

Dim res As HttpWebResponse

Dim XMLStr="...." --some XML doc

req = WebRequest.Create(https://someurl)

req.Method = "POST"

req.ContentType = "text/xml"

'Dim X509Cert As X509Certificate =
X509Certificate.CreateFromCertFile(ConfigurationSettings.AppSettings("CertfilePath"))

Dim X509Cert As X509Certificate =
X509Certificate.CreateFromCertFile("c:\\mycert.der")

req.ClientCertificates.Add(X509Cert)

ServicePointManager.CertificatePolicy = New AcceptAllCertificatesPolicy

Dim reqWriter As StreamWriter = New StreamWriter(req.GetRequestStream())

reqWriter.Write(XMLStr)

reqWriter.Close()

res = req.GetResponse

Dim ResponseXML As String

If res.StatusCode = HttpStatusCode.OK Then

Dim resReader As StreamReader = New StreamReader(res.GetResponseStream)

ResponseXML = resReader.ReadToEnd()

resReader.Close()

Me.TextBox1.Text = ResponseXML

Else

Throw New Exception("Status code =" & res.StatusCode & "XML Request=" &
XMLStr & "XML Response=" & ResponseXML)

End If



Class AcceptAllCertificatesPolicy

Implements ICertificatePolicy

Public Function CheckValidationResult(ByVal srvPoint As
System.Net.ServicePoint, ByVal certificate As
System.Security.Cryptography.X509Certificates.X509Certificate, ByVal request
As System.Net.WebRequest, ByVal certificateProblem As Integer) As Boolean
Implements System.Net.ICertificatePolicy.CheckValidationResult

Return True

End Function

End Class


The same piece of code (identical unmodified) pasted into a win form runs
perfectly without any hitch.

Does anyone have any idea how this could be the case?

Thanks in advance
 
C

C.W.

I have figured out the reason for the error, and have put in place a
workaround.

The reason that the code is failing in asp.net but not in win form is
because asp.net uses aspnet account and my private key certificate is only
installed for my account. Hence by modifying the processModel element of
machine.config file, I was able to run the code successfully. However, that
is not a long term solution. Can anyone point me to some resources on how to
install certificate for aspnet account (or any other account for that
matter).

Thanks in advance

C.W. said:
I am trying to connect to a remote server in order to perform an http post
inside my asp.net page. However, I consistently get the following error:

[SocketException (0x2746): An existing connection was forcibly closed by
the
remote host]
System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32
size, SocketFlags socketFlags)
System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32
size)

[IOException: Unable to read data from the transport connection.]
System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32
size)
System.Net.TlsStream.ForceRead(Byte[] space, Int32 offset, Int32 amount)
System.Net.TlsStream.ReadFullRecord(Byte[] buffer, Int32 length)
System.Net.TlsStream.Handshake(ProtocolToken message)

[WebException: The underlying connection was closed: Could not establish
secure channel for SSL/TLS.]
System.Net.HttpWebRequest.CheckFinalStatus()
System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult)
System.Net.HttpWebRequest.GetRequestStream()
.....


The offending line is Dim reqWriter As StreamWriter = New
StreamWriter(req.GetRequestStream())
The code snippet is as follows:

Dim req As HttpWebRequest

Dim res As HttpWebResponse

Dim XMLStr="...." --some XML doc

req = WebRequest.Create(https://someurl)

req.Method = "POST"

req.ContentType = "text/xml"

'Dim X509Cert As X509Certificate =
X509Certificate.CreateFromCertFile(ConfigurationSettings.AppSettings("CertfilePath"))

Dim X509Cert As X509Certificate =
X509Certificate.CreateFromCertFile("c:\\mycert.der")

req.ClientCertificates.Add(X509Cert)

ServicePointManager.CertificatePolicy = New AcceptAllCertificatesPolicy

Dim reqWriter As StreamWriter = New StreamWriter(req.GetRequestStream())

reqWriter.Write(XMLStr)

reqWriter.Close()

res = req.GetResponse

Dim ResponseXML As String

If res.StatusCode = HttpStatusCode.OK Then

Dim resReader As StreamReader = New StreamReader(res.GetResponseStream)

ResponseXML = resReader.ReadToEnd()

resReader.Close()

Me.TextBox1.Text = ResponseXML

Else

Throw New Exception("Status code =" & res.StatusCode & "XML Request=" &
XMLStr & "XML Response=" & ResponseXML)

End If



Class AcceptAllCertificatesPolicy

Implements ICertificatePolicy

Public Function CheckValidationResult(ByVal srvPoint As
System.Net.ServicePoint, ByVal certificate As
System.Security.Cryptography.X509Certificates.X509Certificate, ByVal
request
As System.Net.WebRequest, ByVal certificateProblem As Integer) As Boolean
Implements System.Net.ICertificatePolicy.CheckValidationResult

Return True

End Function

End Class


The same piece of code (identical unmodified) pasted into a win form runs
perfectly without any hitch.

Does anyone have any idea how this could be the case?

Thanks in advance
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top