Guest User

Untitled

a guest
Jul 18th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. try
  2. {
  3. using (TcpClient client = new TcpClient())
  4. {
  5.  
  6. try
  7. {
  8. client.Connect("gateway.sandbox.push.apple.com", 2195);
  9. Logging("TSSLProDi :Connected to Apple");
  10. }
  11. catch (Exception ex)
  12. {
  13. Logging("TSSLProDi :" + ex.Message + "-IE-" + ex.InnerException);
  14.  
  15. }
  16. using (NetworkStream networkStream = client.GetStream())
  17. {
  18. Logging("TSSLProDi :Client connected.");
  19.  
  20. X509Certificate clientCertificate = new X509Certificate(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory + @"startupcertname.pfx"), "mycertpassword");
  21. X509CertificateCollection clientCertificateCollection = new X509CertificateCollection(new X509Certificate[1] { clientCertificate });
  22.  
  23. // Create an SSL stream that will close the client's stream.
  24. SslStream sslStream = new SslStream(
  25. client.GetStream(),
  26. false,
  27. new RemoteCertificateValidationCallback(validateServerCertificate),
  28. null
  29. );
  30.  
  31. try
  32. {
  33. sslStream.AuthenticateAsClient("gateway.sandbox.push.apple.com", clientCertificateCollection, System.Security.Authentication.SslProtocols.Default, false);
  34. Logging("TSSLProDi :slStreamAuthenticated");
  35. }
  36. catch (AuthenticationException ex)
  37. {
  38. Logging("TSSLProDi :" + "Exception: " + ex.Message.ToString());
  39. if (ex.InnerException != null)
  40. {
  41. Logging("Inner exception: " + ex.InnerException.Message.ToString());
  42. }
  43. Logging("TSSLProDi :" + "Authentication failed - closing the connection.");
  44. client.Close();
  45. return;
  46. }
  47. }
  48.  
  49. }
  50. }
  51. catch (Exception ex)
  52. {
  53.  
  54. Logging("TSSLProCert :" + ex.Message + "-IE-" + ex.InnerException);
  55. }
  56.  
  57. SslStream sslStream = new SslStream(client.GetStream(), false);
Add Comment
Please, Sign In to add comment