2012-02-16 80 views
0

我正在尝试使用c#代码做苹果推送通知。它在我的本地机器上正常工作,我收到通知。但是,当上传到服务器时,我尝试创建新证书时遇到了异常。这是我使用上传到服务器时出现APN错误

protected void sendPayLoad(string message, string eqpt_ID) 
    { 
     try 
     { 
      if (eqpt_ID == string.Empty || eqpt_ID == " (null)") 
      { 
       Log_Error_Message("void sendPayLoad(string message, string eqpt_ID)", "No equipment ID"); 
       return; 
      } 

      int port; 
      String hostname, cert_path, payload; 
      string deviceId, certificatePath, certificatePassword; 
      X509Certificate2 clientCertificate=null; 
      X509Certificate2Collection certificatesCollection=null; 
      TcpClient client=null; 
      SslStream sslStream=null; 
      MemoryStream memoryStream=null; 
      BinaryWriter writer=null; 
      byte[] b1, array; 

      hostname = "gateway.sandbox.push.apple.com";// "gateway.push.apple.com"; 
      cert_path = Session["cert_path"].ToString(); 
      port = 2195; 

      if (cert_path == string.Empty) 
      { 
       Alert.Show("No certificate found for sending message to customer"); 
       return; 
      } 
      //load certificate 
      certificatePath = Server.MapPath(cert_path);//Add p12 certificate here 
      certificatePassword = string.Empty; 

      try 
      { 
       clientCertificate = new X509Certificate2(certificatePath, certificatePassword); 
      } 
      catch (Exception ex) 
      { 
       Log_Error_Message("void sendPayLoad(string message, string eqpt_ID)", "X509Certificate2 error:- \t " + ex.Message); 
       return; 
      } 

      try 
      { 
       certificatesCollection = new X509Certificate2Collection(clientCertificate); 
      } 
      catch (Exception ex) 
      { 
       Log_Error_Message("void sendPayLoad(string message, string eqpt_ID)", "X509Certificate2Collection error:- \t " + ex.Message); 
       return; 
      } 

      try 
      { 
       client = new TcpClient(hostname, port); 
      } 
      catch (Exception ex) 
      { 
       Log_Error_Message("void sendPayLoad(string message, string eqpt_ID)", "TcpClient error:- \t " + ex.Message); 
       return; 
      } 

      try 
      { 
       sslStream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null); 
      } 
      catch (Exception ex) 
      { 
       Log_Error_Message("void sendPayLoad(string message, string eqpt_ID)", "SslStream or RemoteCertificateValidationCallback error:- \t " + ex.Message); 
       return; 
      } 

      try 
      { 
       sslStream.AuthenticateAsClient(hostname, certificatesCollection, SslProtocols.Tls, true); 
      } 
      catch (AuthenticationException ex) 
      { 
       client.Close(); 
       Log_Error_Message("void sendPayLoad(string message, string eqpt_ID)", "AuthenticationException:\n\t" + ex.Message); 
       return; 
      } 

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


      deviceId = eqpt_ID; 

      writer.Write(HexToData(deviceId.ToUpper())); 

      payload = "{\"aps\":{\"alert\":\"" + message + "\",\"badge\":1}}"; 

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

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

      array = memoryStream.ToArray(); 
      sslStream.Write(array); 
      sslStream.Flush(); 

      //String deviceId = "DEVICEIDGOESHERE"; 


      // Close the client connection. 
      client.Close(); 
     } 
     catch (Exception ex) 
     {   
      Cre_Log_Err_takeaway.Append_Err_Msg("rest/booking.aspx_page", "void sendPayLoad(string message, string eqpt_ID)", ex.Message); 
     } 
    } 

虽然试图在服务器上运行我正在一个excception在该行

try 
      { 
       clientCertificate = new X509Certificate2(certificatePath, certificatePassword); 
      } 
      catch (Exception ex) 
      { 
       Log_Error_Message("void sendPayLoad(string message, string eqpt_ID)", "X509Certificate2 error:- \t " + ex.Message); 
       return; 
      } 

在日志中,我只得到

的内部代码发生错误

我已打印出证书路径及其指向这条道路

d:\主机\ 8842886 \ HTML \ testwebsite \ certificate_201212725953Pushcerti.p12

而且这个证书我没有任何密码,所以我用Sting.Empty我也尝试通过这样的空字符串“”。仍然没有运气。 我以前在活服务器中使用过这个代码,它工作正常。这次我使用的是godaddy服务器。

我该如何正确地做到这一点?

感谢

+0

除了您发布你得到了什么错误,例如,如果你要在服务器上调试代码..也是你所谈论的服务器上的所有代码是否有d:驱动器你可否确认...?你还可以比较工作机器上证书与非机器证书的区别。必须有一些不同的小东西..它可能在所有应有的尊重中正视你的脸。也检查.config文件设置或Machine.Config设置只是为了确保.... – MethodMan 2012-02-16 20:17:06

+0

结帐此链接以及它可能会帮助您找到一个快速解决方案http://social.msdn.microsoft.com/Forums/en-AU/csharplanguage/thread/4011d0ea-3dba-4d04-b6cf-adada7956f02 – MethodMan 2012-02-16 20:19:51

回答

0

我已经找到了解决这个:

解决方案1:applicationpool为 “LOCALSERVICE” 的设置身份 - 我不喜欢(!!)

解决方案2:在X509Certificate2类的构造函数中使用标志X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable

MS: x509keystorageflags

相关问题