Socket and Authentication failed because the remote party has closed the transport stream exception in WPF

13,407

Got it working.

The server who is hosting the socket server MUST have installed on its Certification Storage the certificate WITH the Private Key. If you install it without it (just the certificate or just the public key) you will get those errors of authentication failure.

I hope it help.

Share:
13,407
Zozo
Author by

Zozo

Just Zozo ;)

Updated on June 21, 2022

Comments

  • Zozo
    Zozo about 2 years

    I am trying to connect with Java netty based server, which auto generates certificates for itself (and server guys told me, that is accepting any certificate from client side for now).

    My task was to migrate TcpSocket connection into Tls encrypted connection.

    First of all, I converted TcpSocket into NetworkStream:

    using (var client = new NetworkStream(connection.TcpSocket))
    {
        if (client.CanRead)
        {
            client.BeginRead(recvState.DataBuffer, 0, recvState.DataBuffer.Length, ReceiveCallback,
            recvState);
        }
    }
    

    and that is working perfectly. So then, I decided to build SslAuthentication - like here:

    using (var client = new NetworkStream(connection.TcpSocket))
    using (var sslStream = new SslStream(client, false, App_CertificateValidation))
    {
        var clientCertificate = new X509Certificate2("client.pfx");
        var clientCertificateCollection = new X509Certificate2Collection(new[] { clientCertificate });
        sslStream.AuthenticateAsClient("MyServer", clientCertificateCollection, SslProtocols.Tls, false);
    
        if (sslStream.CanRead)
        {
            sslStream.BeginRead(recvState.DataBuffer, 0, recvState.DataBuffer.Length, ReceiveCallback,
            recvState);
        }
    }
    

    Where client.pfx is random certificate with no password, as a file in project and also imported into Current User Certificates > Personal > Certificates in certmgr.msc.

    The problem is AuthenticateAsClient throws an

    System.IO.IOException: Authentication failed because the remote party has closed the transport stream exception.

    Also, if the hostname in AuthenticateAsCtlient method means anything, if server accepts every certificate? Should I put there something significant?

    I still can contact with the server guys, so I can ask them about everything - do we need any additional information?

  • Zozo
    Zozo over 9 years
    I am going to pass this answer to our server team. Thank you!
  • Rajdeep Paliwal
    Rajdeep Paliwal almost 9 years
    my application sends mail for 8 to 10 hours then it throws exception like this "Authentication failed because the remote party has closed the transport stream." how to deal with this case? please help.
  • Brandon
    Brandon over 7 years
    Dude, you just saved my ass with this post. Thank you so much! I would totally give you ALL of my reputation points right now if I could! lol!