IOS Push Notification Problem - Authentication failed because the remote side closed the transport stream

I use the code below for push notification.

public void PushNotificationIOS(string message, string registrationKey) { string deviceID = registrationKey; int port = 2195; String hostname = System.Configuration.ConfigurationManager.AppSettings["HostName"];//"gateway.sandbox.push.apple.com"; string certPath = string.Empty; certPath = System.Configuration.ConfigurationManager.AppSettings["CertificatePath"] + System.Configuration.ConfigurationManager.AppSettings["Cer String certificatePath = System.Web.HttpContext.Current.Server.MapPath(certPath); string certPassword = System.Configuration.ConfigurationManager.AppSettings["CertificatePassword"]; TcpClient client = new TcpClient(hostname, port); try { X509Certificate2 clientCertificate = new X509Certificate2(System.IO.File.ReadAllBytes(certificatePath), certPassword); X509Certificate2Collection certificatesCollection = new X509Certificate2Collection(clientCertificate); SslStream sslStream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null); sslStream.AuthenticateAsClient(hostname, certificatesCollection, SslProtocols.Tls, false); MemoryStream memoryStream = new MemoryStream(); BinaryWriter writer = new BinaryWriter(memoryStream); writer.Write((byte)0); writer.Write((byte)0); writer.Write((byte)32); writer.Write(StringToByteArray(deviceID.ToUpper())); String payload = "{\"aps\":{\"alert\":\"" + message + "\",\"badge\":0,\"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(); } } 

I get the following error Authentication failed because the remote side has closed the transport stream. In Trace, I get below

 System.Net.Security.SslState.CheckThrow(Boolean authSucessCheck) at System.Net.Security.SslState.get_SecureStream() at System.Net.Security.SslStream.Write(Byte[] buffer) 

I tried everything that was mentioned in different posts, but cannot solve the problem.

+6
source share
1 answer

Verify that the certificate is valid and not damaged. You can restore the certificate to be sure.

Try to provide all SecurityProtocolType parameters, for example:

 sslStream.AuthenticateAsClient(hostname, certificatesCollection, SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls, false); 

SecurityProtocolType.Tls12 can negotiate the security level of transport layer 1.1 or down, but try all the options to be sure.

enter image description here

For more information about SecurityProtocolType see below:

https://msdn.microsoft.com/en-us/library/system.net.securityprotocoltype(v=vs.110).aspx

+1
source

All Articles