Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > The underlying connection was closed: Could not establish secure channel for SSL/TLS

Reply
Thread Tools

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

 
 
C.W.
Guest
Posts: n/a
 
      08-15-2005
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(IAsy ncResult 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(ConfigurationSe ttings.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.X509 Certificate, ByVal request
As System.Net.WebRequest, ByVal certificateProblem As Integer) As Boolean
Implements System.Net.ICertificatePolicy.CheckValidationResul t

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



 
Reply With Quote
 
 
 
 
C.W.
Guest
Posts: n/a
 
      08-15-2005
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." <c.w@nospam_com> wrote in message
news:(E-Mail Removed)...
>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(IAsy ncResult 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(ConfigurationSe ttings.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.X509 Certificate, ByVal
> request
> As System.Net.WebRequest, ByVal certificateProblem As Integer) As Boolean
> Implements System.Net.ICertificatePolicy.CheckValidationResul t
>
> 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
>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel. Joel Barsotti ASP .Net 4 04-19-2007 11:12 PM
The underlying connection was closed: Could not establish secure channel for SSL/TLS. Jim Butler ASP .Net 7 07-12-2006 01:37 AM
Again: The underlying connection was closed: Could not establish secure channel for SSL/TLS jsanjosem@gmail.com ASP .Net Web Services 2 12-07-2005 10:11 AM
The underlying connection was closed: Could not establish secure channel for SSL/TLS C.W. ASP .Net Web Services 1 08-15-2005 04:23 AM
The underlying connection as closed: Could not establish secure channel for SSL/TLS Ghislain Tanguay ASP .Net Web Services 3 09-03-2004 07:49 PM



Advertisments