2

我知道以前有人问过这个问题,但这些问题似乎都对我没有帮助。

我正在尝试从我的 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 的某个地方注册证书?

最好的问候,马吕斯。

4

0 回答 0