a call to sspi failed see inner exception. the message received was unexpected or badly formatted in windows 7]

2k views Asked by At

I am getting the following error " a call to sspi failed see inner exception. the message received was unexpected or badly formatted in windows 7". i tried all the possible ways but no luck. please check it

Here is the code

        int port = 2195;
        String deviceID = "d6c597fcc4e3426993cf29a3a8857efbed3462d5d8e9e32c0f8b387djkdklldk";
        String hostname = "gateway.sandbox.push.apple.com";     // TEST
        //String hostname = "gateway.push.apple.com";           // REAL
        string p12FilePassword = "password";
        //        @"cert.p12";
        String certificatePath = HttpContext.Current.Server.MapPath("~/Certificate.p12");


        X509Certificate2 clientCertificate = string.IsNullOrEmpty(p12FilePassword) ? new X509Certificate2(File.ReadAllBytes(certificatePath)) : new X509Certificate2(File.ReadAllBytes(certificatePath), p12FilePassword, X509KeyStorageFlags.MachineKeySet);

        X509Certificate2Collection certificatesCollection = new X509Certificate2Collection(clientCertificate);
        TcpClient client = new TcpClient(hostname, port);

        SslStream sslStream = new SslStream(client.GetStream(), false, ValidateServerCertificate, SelectLocalCertificate);
        try
        {
            sslStream.AuthenticateAsClient(hostname, certificatesCollection, System.Security.Authentication.SslProtocols.Tls, false);
        }
        catch (Exception e)
        {
            throw (e);
            //client.Close();
            //return;
        }
1

There are 1 answers

0
Natalya On

I see this is an old post but I recently faced this problem. Application can work on my Windows10 and failed on Windows server 2008r2 and 2012r2. The solution is get rid off SslStream and use libraries like openssl or bouncycastle for tls connections.