APNS推送通知错误 - 对SSPI的调用失败,请参阅内部异常

时间:2014-09-18 11:00:12

标签: asp.net error-handling apple-push-notifications sspi

我知道之前已经问过这个问题,但这似乎都没有帮助我。

我正在尝试从我的asp.net应用程序发送推送通知,但是我收到以下错误:

"A call to SSPI failed, see inner exception." System.Exception {System.Security.Authentication.AuthenticationException}

"The message received was unexpected or badly formatted" System.Exception {System.ComponentModel.Win32Exception}

以下是代码:

int port = 2195;
String hostname = "gateway.sandbox.push.apple.com";

//load certificate
string certificatePath = HostingEnvironment.ApplicationPhysicalPath + "cert.pem";
string certificatePassword = "xxxxxxx";

X509Certificate2 clientCertificate = new X509Certificate2(certificatePath, certificatePassword);
X509Certificate2Collection certificatesCollection = new X509Certificate2Collection(clientCertificate);

TcpClient client = new TcpClient(hostname, port);
SslStream sslStream = new SslStream(
                        client.GetStream(),
                        false,
                        null,
                        null
        );

try
{
    sslStream.AuthenticateAsClient(hostname, certificatesCollection, System.Security.Authentication.SslProtocols.Tls, false);
}
catch (AuthenticationException ex)
{
    client.Close();
}

// 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 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
writer.Write((deviceId.ToUpper().ToArray()));

String payload = "{\"aps\":{\"alert\":\"Test\",\"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();

我的证书有问题,或者为什么会这样?

我应该使用p12文件吗?如果是这样,如果我有.pem文件,我如何获得p12文件?

我在Windows上开发这个,但我应该在Windows的某个地方注册证书?

祝你好运, 马吕斯。

0 个答案:

没有答案