Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net (http://www.velocityreviews.com/forums/f29-asp-net.html)
-   -   The underlying connection was closed: Could not establish secure channel for SSL/TLS (http://www.velocityreviews.com/forums/t108042-the-underlying-connection-was-closed-could-not-establish-secure-channel-for-ssl-tls.html)

C.W. 08-15-2005 02:34 AM

The underlying connection was closed: Could not establish secure channel for SSL/TLS
 
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




C.W. 08-15-2005 04:23 AM

Re: The underlying connection was closed: Could not establish secure channel for SSL/TLS
 
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:OZxAFHUoFHA.3936@TK2MSFTNGP10.phx.gbl...
>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
>
>
>





All times are GMT. The time now is 06:20 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.