Member 12183079 Ответов: 0

Как исправить эту проблему при неудачном вызове SSPI, см. Внутреннее исключение.


public void PushNotificationToApple(string deviceID)
        {
            int port = 2195;
            String hostname = "gateway.sandbox.push.apple.com";

            //load certificate
            string certificatePath = HttpContext.Current.Server.MapPath("MPEDA_Production_Certificates.pem");          
            string certificatePassword = "xxxxx";
            X509Certificate2 clientCertificate = new X509Certificate2(certificatePath, certificatePassword);
            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, SslProtocols.Tls, true);//Exception Line

            }
            catch (AuthenticationException ex)
            {
                client.Close();
                return;
            }

            // Encode a test message into a byte array.
            MemoryStream memoryStream = new MemoryStream();
            BinaryWriter 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)

            String deviceId = "DEVICEIDGOESHERE";
            writer.Write(ToByteArray(deviceId.ToUpper()));

            String payload = "{\"aps\":{\"alert\":\"I like spoons also\",\"badge\":14}}";

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

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

            byte[] array = memoryStream.ToArray();
            sslStream.Write(array);
            sslStream.Flush();

            // Close the client connection.
            client.Close();
        }


        public static byte[] ToByteArray(string hex)
        {
            return Enumerable.Range(0, hex.Length)
                             .Where(x => x % 2 == 0)
                             .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
                             .ToArray();
        }

        public static bool ValidateServerCertificate(
        object sender,
        X509Certificate certificate,
        X509Chain chain,
        SslPolicyErrors sslPolicyErrors)
        {
            if (sslPolicyErrors == SslPolicyErrors.None)
                return true;

            Console.WriteLine("Certificate error: {0}", sslPolicyErrors);

            // Do not allow this client to communicate with unauthenticated servers.
            return false;
        }


Что я уже пробовал:

Привет
Я пытаюсь отправить Push-уведомление на IOS и написать код выше упоминания

но я получаю
Exception sslStream.AuthenticateAsClient(hostname, certificatesCollection, SslProtocols.Tls, true);


SSl нет на моем сервере поэтому пожалуйста помогите мне как решить эту проблему

Richard MacCutchan

Я думаю, что сообщение "смотрите внутреннее исключение" - это ключ к разгадке.

Member 12183079

так как же это исправить

Richard MacCutchan

Сначала вам нужно выяснить, что сломано; и никто здесь не может догадаться, каковы ваши детали исключения.

Dave Kreskowiak

Когда вы получаете исключение, оно имеет свойство, называемое, как ни странно, "InnerException". Посмотрите на это исключение для деталей.

0 Ответов