2012-02-16 7 views
0

C#コードを使用してAppleプッシュ通知をしようとしています。私のローカルマシンで正常に動作しており、通知を受けています。しかし、サーバーにアップロードすると、新しい証明書を作成しようとすると例外が発生します。これは、サーバーで実行しようとしている私は、このラインで私が唯一サーバーにアップロードしたときのAPNエラー

アン内部を取得していますログに

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; 
      } 

をexcceptionを取得していますが、私は

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); 
     } 
    } 

を使用していたコードですエラーが発生しました

証明書のパスとそのポインティングこのパスに

D:また\ 8842886 \ HTML \ testwebsite \ certificate_201212725953Pushcerti.p12

ホスティング\この証明書のために、私は任意のパスワードを持っていけない、だから私は、私はまた、Sting.Emptyを使用していますこのような空の文字列を渡そうとしました""。 ライブサーバーでこのコードを使用していて、うまくいきました。今回私はgodaddyサーバーを使用しています。

これを正しく行うにはどうすればよいですか?

解決方法1::私はこのための解決策を発見した

おかげ

+0

あなたは確認できますか?作業機械上の証明書とそうでない証明書の違いを比較することもできますか。それは少し異なっている必要があります..それはおそらくすべての面であなたを正面から見ている..確かに.configファイル設定またはMachine.Config設定を確認してください.... – MethodMan

+0

このリンクもチェックアウトクイックソリューションにつながる可能性があります。http://social.msdn.microsoft.com/Forums/en-AU/csharplanguage/thread/4011d0ea-3dba-4d04-b6cf-adada7956f02 – MethodMan

答えて

0

"LocalServiceと" へapplicationpoolの設定アイデンティティ - 私が好むではないでしょう(!!)

解決策2: X509Certificate2クラスのコンストラクタにフラグX509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportableを使用します。ドライブ:さておき、あなたが話しているサーバー上のサーバー上のコード..もがDがあることをデバッグした場合、あなたは例えばどんなエラーが出るん投稿されたすべてのそのコードから

MS: x509keystorageflags

関連する問題