Issue in IOS Push Notification - Authentication failed because the remote party has closed the transport stream
Asked Answered
T

1

6

I am using the below code 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 am getting the following error Authentication failed because the remote party has closed the transport stream. On Trace, I am getting the 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 all the things mentioned in various post but am not able to solve the issue.

Tiger answered 20/4, 2016 at 9:24 Comment(3)
which version of .Net you are using 4.0 or 4.5 or 4.6?Lollar
it is MVC 4.5 framework. Visual studio 2015Tiger
Error removed when I create certificate using private key only. Also, give permission to the folder in which .p12 is lied.Tiger
L
1

Make sure certificate is valid & is not corrupted. You can regenerate certificate to be dead sure.

Try providing all the options for SecurityProtocolType like :

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

SecurityProtocolType.Tls12 can negotiate Transport layer security 1.1 or downwards too but try all options to be sure.

enter image description here

For more details on SecurityProtocolType refer :

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

Lollar answered 27/4, 2016 at 14:30 Comment(2)
I already created new certificate also tried the certificate on iphone itself and notification is working correctly. I also used the below code. try{ sslStream.AuthenticateAsClient(hostname, certificatesCollection, SslProtocols.Tls, false); } catch(exception e) { mylog(e.message); } try{ sslStream.AuthenticateAsClient(hostname, certificatesCollection, SslProtocols.Tls11, false); } catch(exception e) { mylog(e.message); } try{ sslStream.AuthenticateAsClient(hostname, certificatesCollection, SslProtocols.Tls12, false); } catch(exception e) { mylog(e.message); } In every catch I got same error.Tiger
Just don't write all variations at same time try one by one. OR Use Pipe symbol...Lollar

© 2022 - 2024 — McMap. All rights reserved.