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

Discussion in 'ASP .Net Web Services' started by C.W., Aug 15, 2005.

  1. C.W.

    C.W. Guest

    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.W., Aug 15, 2005
    #1
    1. Advertising

  2. C.W.

    C.W. Guest

    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:...
    >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.W., Aug 15, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. C.W.
    Replies:
    1
    Views:
    5,932
  2. Jim Butler
    Replies:
    7
    Views:
    7,383
    Steven Cheng[MSFT]
    Jul 12, 2006
  3. Joel Barsotti
    Replies:
    4
    Views:
    6,519
    Joel Barsotti
    Apr 20, 2007
  4. Ghislain Tanguay
    Replies:
    3
    Views:
    199
    suresh g
    Sep 3, 2004
  5. Replies:
    2
    Views:
    179
Loading...

Share This Page