【问题标题】:iOS (APN) - Authentication failed because the remote party has closed the transport stream in C#iOS (APN) - 身份验证失败,因为远程方已关闭 C# 中的传输流
【发布时间】:2015-09-22 07:18:28
【问题描述】:

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

       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 { }
        }

【问题讨论】:

  • 这个错误发生在代码的哪一行?

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


【解决方案1】:

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

我建议使用PushSharp 节省时间并摆脱混乱的编码。

PushSharp 是一个服务器端库,用于向 iOS(iPhone/iPad APNS)、Android(C2DM 和 GCM - Google Cloud Message)、Windows Phone、Windows 8、Amazon、Blackberry 和(即将推出的)FirefoxOS 设备发送推送通知!

【讨论】:

    猜你喜欢
    • 2015-09-28
    • 2017-08-18
    • 2015-08-20
    • 2013-03-24
    • 2022-06-29
    • 1970-01-01
    • 1970-01-01
    • 2017-02-18
    相关资源
    最近更新 更多