iOS(APN) - 身份验证失败,因为远程方已在C#中关闭传输流

时间:2015-09-22 07:18:28

标签: c# openssl apple-push-notifications x509certificate2 apn

从C#代码向Apple手机发送推送通知时,我收到此错误。我搜索了这个问题,但这些答案无法解决这个问题。 任何人都可以给我建议解决这个问题。

       protected void Page_Load(object sender, EventArgs e)
        {
            string deviceID = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
            string certificateFilePath ="XXXXXXX.cer";
            pushMessage(deviceID, certificateFilePath);
        }
        public void pushMessage(string deviceID, string certificateFilePath)
        {
            try
            {
                int port = 2195;
                String hostname = "gateway.sandbox.push.apple.com";
                String certificatePath = HttpContext.Current.Server.MapPath(certificateFilePath);
                X509Certificate2 clientCertificate = new X509Certificate2(certificatePath);
                X509Certificate2Collection certificatesCollection = new X509Certificate2Collection(clientCertificate);
                TcpClient client = new TcpClient(hostname, port);
                SslStream sslStream = new SslStream(
                        client.GetStream(),
                        false,
                        new RemoteCertificateValidationCallback(ValidateServerCertificate),
                        null
                );
                try
                {
                   sslStream.AuthenticateAsClient(hostname, certificatesCollection, System.Security.Authentication.SslProtocols.Default, true);
                //sslStream.AuthenticateAsClient(hostname, certificatesCollection, System.Security.Authentication.SslProtocols.Default, false);
                //sslStream.AuthenticateAsClient(hostname, certificatesCollection, System.Security.Authentication.SslProtocols.Tls, true);
                }
                catch (AuthenticationException ex)
                {
                    client.Close();
                    return;
                }
                MemoryStream memoryStream = new MemoryStream();
                BinaryWriter writer = new BinaryWriter(memoryStream);
                writer.Write((byte)0);  
                writer.Write((byte)0); 
                writer.Write((byte)32); 
                String deviceId = deviceID;
                writer.Write(HexStringToByteArray(deviceId.ToUpper()));
                String payload = "{\"aps\":{\"alert\":\"Test...\",\"badge\":1}}";
                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 { }
        }

1 个答案:

答案 0 :(得分:0)

您是否提供证书密码以验证您的证书?

我建议使用PushSharp节省时间和杂乱的编码。

PushSharp是一个服务器端库,用于向iOS(iPhone / iPad APNS),Android(C2DM和GCM - 谷歌云消息),Windows Phone,Windows 8,亚马逊,黑莓和(很快)FirefoxOS设备发送推送通知!