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

Posted

技术标签:

【中文标题】iOS (APN) - 身份验证失败,因为远程方已关闭 C# 中的传输流【英文标题】:iOS (APN) - Authentication failed because the remote party has closed the transport stream in 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  
        

【问题讨论】:

这个错误发生在代码的哪一行? 【参考方案1】:

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

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

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

【讨论】:

以上是关于iOS (APN) - 身份验证失败,因为远程方已关闭 C# 中的传输流的主要内容,如果未能解决你的问题,请参考以下文章

推送通知上的错误“身份验证失败,因为远程方已关闭传输流”

APNS SSlStream 身份验证失败,因为远程方已关闭传输流

HttpWebrequest 失败,内部异常身份验证失败,因为远程方已关闭传输流

普通用户的“身份验证失败,因为远程方已关闭传输流”

身份验证失败,因为远程方已关闭传输流

C# 身份验证失败,因为远程方已关闭传输流