Click here to Skip to main content
15,902,112 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
getting error as "Authentication failed because the remote party has closed the transport stream."

What I have tried:

C#
int port = 2195;
           string hostname = "gateway.sandbox.push.apple.com";
         //  String hostname = "gateway.push.apple.com";
           String certificatePath = Server.MapPath("PushnotificationDev.p12");
           X509Certificate2 clientCertificate = new X509Certificate2(System.IO.File.ReadAllBytes(certificatePath), "password");
           X509Certificate2Collection certificatesCollection = new X509Certificate2Collection(clientCertificate);

           TcpClient client = new TcpClient(hostname, port);
           SslStream sslStream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null);

           try
           {
              // client.Connect(hostname,port);
               //sslStream.AuthenticateAsServer(hostname, certificatesCollection, SslProtocols.Default, true);
               ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
               sslStream.AuthenticateAsServer(clientCertificate, false, SslProtocols.Ssl3, true);
               MemoryStream memoryStream = new MemoryStream();
               BinaryWriter writer = new BinaryWriter(memoryStream);
               writer.Write((byte)0);
               writer.Write((byte)0);
               writer.Write((byte)32);

               writer.Write(ConvertToByteArray("9865ac6c888e9b9e819b15772e6bcb11d64c659d331a28cf7674050a20fa895p"));
               String payload = "{\"aps\":{\"alert\":\"" + "Hi,, This Is a Sample Push Notification For IPhone.." + "\",\"badge\":1,\"sound\":\"default\"}}";
               writer.Write((byte)0);
               writer.Write((byte)payload.Length);
               byte[] b1 = System.Text.Encoding.UTF8.GetBytes(payload);
               writer.Write(b1);
               writer.Flush();
               byte[] array = memoryStream.ToArray();
               sslStream.Write(array);
               sslStream.Flush();
               client.Close();
           }
           catch (System.Security.Authentication.AuthenticationException ex)
           {
               client.Close();
           }
           catch (Exception ex)
           {
               lblenc.Text = ex.Message;
               client.Close();
           }
Posted
Updated 28-Dec-16 2:24am

1 solution

If you google the error you'll find that there are many possible reasons for this. You'll need to work through the possible reasons until you find the one causing it. You'll have to troubleshoot this on your own.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900