Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# iPhone push server?

Im attempting to write a push server for the iPhone in C#. I have the following code:

        // Create a TCP/IP client socket.         using (TcpClient client = new TcpClient())         {             client.Connect("gateway.sandbox.push.apple.com", 2195);             using (NetworkStream networkStream = client.GetStream())             {                 Console.WriteLine("Client connected.");                  X509Certificate clientCertificate = new X509Certificate(@"certfile.p12", passwordHere);                 X509CertificateCollection clientCertificateCollection = new X509CertificateCollection(new X509Certificate[1] { clientCertificate });                  // Create an SSL stream that will close the client's stream.                 SslStream sslStream = new SslStream(                     client.GetStream(),                     false,                     new RemoteCertificateValidationCallback(ValidateServerCertificate),                     null                     );                  try                 {                     sslStream.AuthenticateAsClient("gateway.sandbox.push.apple.com");                 }                 catch (AuthenticationException e)                 {                     Console.WriteLine("Exception: {0}", e.Message);                     if (e.InnerException != null)                     {                         Console.WriteLine("Inner exception: {0}", e.InnerException.Message);                     }                     Console.WriteLine("Authentication failed - closing the connection.");                     client.Close();                     return;                 }             } 

ect....

Only I keep receiving a exception: "A call to SSPI failed, see Inner exception" Inner Exception -> "The message received was unexpected or badly formatted."

Does anyone have any idea whats going wrong here?

like image 724
Kyle Avatar asked Jun 28 '09 23:06

Kyle


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

Figured it out. Replaced sslStream.AuthenticateAsClient("gateway.sandbox.push.apple.com"); with sslStream.AuthenticateAsClient("gateway.sandbox.push.apple.com", clientCertificateCollection, SslProtocols.Default, false); And registered the certificates on the PC.

Edit: Here is the code for creating a payload as requested:

    private static byte[] GeneratePayload(byte [] deviceToken, string message, string sound)     {         MemoryStream memoryStream = new MemoryStream();          // Command         memoryStream.WriteByte(0);          byte[] tokenLength = BitConverter.GetBytes((Int16)32);         Array.Reverse(tokenLength);         // device token length         memoryStream.Write(tokenLength, 0, 2);          // Token         memoryStream.Write(deviceToken, 0, 32);          // String length         string apnMessage = string.Format ( "{{\"aps\":{{\"alert\":{{\"body\":\"{0}\",\"action-loc-key\":null}},\"sound\":\"{1}\"}}}}",             message,             sound);          byte [] apnMessageLength = BitConverter.GetBytes((Int16)apnMessage.Length);         Array.Reverse ( apnMessageLength );         // message length         memoryStream.Write(apnMessageLength, 0, 2);          // Write the message         memoryStream.Write(System.Text.ASCIIEncoding.ASCII.GetBytes(apnMessage), 0, apnMessage.Length);          return memoryStream.ToArray();     } // End of GeneratePayload 
like image 109
Kyle Avatar answered Sep 23 '22 07:09

Kyle