IOS 推送通知中的问题 - 身份验证失败,因为远程方已关闭传输流

Posted

技术标签:

【中文标题】IOS 推送通知中的问题 - 身份验证失败,因为远程方已关闭传输流【英文标题】:Issue in IOS Push Notification - Authentication failed because the remote party has closed the transport stream 【发布时间】:2016-04-20 09:24:00 【问题描述】:

我正在使用以下代码进行推送通知。

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();
    


我收到以下错误 身份验证失败,因为远程方已关闭传输流。 在 Trace 上,我得到以下信息

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

我尝试了各种帖子中提到的所有方法,但无法解决问题。

【问题讨论】:

您使用的是哪个版本的 .Net 4.0 或 4.5 或 4.6? 它是 MVC 4.5 框架。视觉工作室 2015 仅使用私钥创建证书时,错误已消除。另外,授予.p12所在文件夹的权限。 【参考方案1】:

确保证书有效且未损坏。您可以重新生成证书以确保万无一失。

尝试为SecurityProtocolType 提供所有选项,例如:

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

SecurityProtocolType.Tls12 也可以协商传输层安全性 1.1 或更低版本,但请尝试所有选项以确保。

有关SecurityProtocolType的更多详细信息,请参阅:

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

【讨论】:

我已经创建了新证书,也尝试了 iphone 本身的证书并且通知工作正常。我还使用了下面的代码。尝试 sslStream.AuthenticateAsClient(主机名,certificatesCollection,SslProtocols.Tls,false); 捕捉(异常 e) mylog(e.message); try sslStream.AuthenticateAsClient(主机名,certificatesCollection,SslProtocols.Tls11,false); 捕捉(异常 e) mylog(e.message); try sslStream.AuthenticateAsClient(主机名,certificatesCollection,SslProtocols.Tls12,false); 捕捉(异常 e) mylog(e.message); 在每次捕获中我都遇到了同样的错误。 只是不要同时写所有变体,一一尝试。或使用管道符号...

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

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

未在工作灯中使用身份验证的推送通知错误

尝试推送通知

如何在工作灯中为 Windows Phone 8 配置未经身份验证的推送通知

使用 php 在 android 和 ios 上使用 Infusionsoft 发送推送通知

Google Drive API - 未收到推送通知(基本身份验证)