上传到服务器时出现APN错误

时间:2012-02-16 20:03:35

标签: c# ios apple-push-notifications x509certificate

我正在尝试使用c#代码进行苹果推送通知。它在我的本地机器上工作正常,我收到通知。但是当上传到服务器时,我在尝试创建新证书时遇到异常。这是我正在使用的代码

protected void sendPayLoad(string message, string eqpt_ID)
    {
        try
        {
            if (eqpt_ID == string.Empty || eqpt_ID == " (null)")
            {
                Log_Error_Message("void sendPayLoad(string message, string eqpt_ID)", "No equipment ID");
                return;
            }

            int port;
            String hostname, cert_path, payload;
            string deviceId, certificatePath, certificatePassword;
            X509Certificate2 clientCertificate=null;
            X509Certificate2Collection certificatesCollection=null;
            TcpClient client=null;
            SslStream sslStream=null;
            MemoryStream memoryStream=null;
            BinaryWriter writer=null;
            byte[] b1, array;

            hostname = "gateway.sandbox.push.apple.com";// "gateway.push.apple.com";
            cert_path = Session["cert_path"].ToString();
            port = 2195;

            if (cert_path == string.Empty)
            {
                Alert.Show("No certificate found for sending message to customer");
                return;
            }
            //load certificate
            certificatePath = Server.MapPath(cert_path);//Add p12 certificate here
            certificatePassword = string.Empty;

            try
            {
                clientCertificate = new X509Certificate2(certificatePath, certificatePassword);
            }
            catch (Exception ex)
            {
                Log_Error_Message("void sendPayLoad(string message, string eqpt_ID)", "X509Certificate2 error:- \t " + ex.Message);
                return;
            }

            try
            {
                certificatesCollection = new X509Certificate2Collection(clientCertificate);
            }
            catch (Exception ex)
            {
                Log_Error_Message("void sendPayLoad(string message, string eqpt_ID)", "X509Certificate2Collection error:- \t " + ex.Message);
                return;
            }

            try
            {
                client = new TcpClient(hostname, port);
            }
            catch (Exception ex)
            {
                Log_Error_Message("void sendPayLoad(string message, string eqpt_ID)", "TcpClient error:- \t " + ex.Message);
                return;
            }

            try
            {
                sslStream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null);
            }
            catch (Exception ex)
            {
                Log_Error_Message("void sendPayLoad(string message, string eqpt_ID)", "SslStream or RemoteCertificateValidationCallback error:- \t " + ex.Message);
                return;
            }

            try
            {
                sslStream.AuthenticateAsClient(hostname, certificatesCollection, SslProtocols.Tls, true);
            }
            catch (AuthenticationException ex)
            {
                client.Close();
                Log_Error_Message("void sendPayLoad(string message, string eqpt_ID)", "AuthenticationException:\n\t" + ex.Message);
                return;
            }

            // Encode a test message into a byte array.
            memoryStream = new MemoryStream();
            writer = new BinaryWriter(memoryStream);

            writer.Write((byte)0);  //The command
            writer.Write((byte)0);  //The first byte of the deviceId length (big-endian first byte)
            writer.Write((byte)32); //The deviceId length (big-endian second byte)


            deviceId = eqpt_ID;

            writer.Write(HexToData(deviceId.ToUpper()));

            payload = "{\"aps\":{\"alert\":\"" + message + "\",\"badge\":1}}";

            writer.Write((byte)0); //First byte of payload length; (big-endian first byte)
            writer.Write((byte)payload.Length);     //payload length (big-endian second byte)

            b1 = System.Text.Encoding.UTF8.GetBytes(payload);
            writer.Write(b1);
            writer.Flush();

            array = memoryStream.ToArray();
            sslStream.Write(array);
            sslStream.Flush();

            //String deviceId = "DEVICEIDGOESHERE";


            // Close the client connection.
            client.Close();
        }
        catch (Exception ex)
        {           
            Cre_Log_Err_takeaway.Append_Err_Msg("rest/booking.aspx_page", "void sendPayLoad(string message, string eqpt_ID)", ex.Message);
        }
    }

尝试在服务器上运行时,我在这一行得到了一个感受

 try
            {
                clientCertificate = new X509Certificate2(certificatePath, certificatePassword);
            }
            catch (Exception ex)
            {
                Log_Error_Message("void sendPayLoad(string message, string eqpt_ID)", "X509Certificate2 error:- \t " + ex.Message);
                return;
            }

在日志中我只是

  

发生内部错误

我已打印出证书路径及其指向此路径

  

d:\主机\ 8842886 \ HTML \ testwebsite \ certificate_201212725953Pushcerti.p12

同样对于这个证书,我没有任何密码,所以我使用 Sting.Empty 我也试过传递一个像“”这样的空字符串。但是没有运气。 我之前在实时服务器中使用过此代码并且工作正常。这次我使用的是godaddy服务器。

我该如何正确地做到这一点?

由于

1 个答案:

答案 0 :(得分:0)

我找到了解决方法:

解决方案1:将应用程序池的标识设置为" LOCALSERVICE" - 我不喜欢(!!)

解决方案2:在X509Certificate2类的构造函数中使用标志X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable

MS: x509keystorageflags