ComponentPro UltimateFtp

      CertificateReceived Event

      See AlsoMembers Options: Show AllLanguage Filter: AllSend comments on this topic to ComponentPro
      Occurs when an FTP server's certificate was received and verified.

      Syntax

      public event EventHandler<TEventArgs> CertificateReceived

      Examples

      Show how to handle the CertificateReceived event.

      using System;
      using System.Security.Cryptography.X509Certificates;
      using System.Text;
      using ComponentPro.Net;
      using ComponentPro.Security.Certificates;
      
      ...
      
      static void Main()
      {
          // Create a new class instance. 
          Ftp client = new Ftp();
      
          client.CertificateReceived += client_CertificateReceived;
      
          // Connect to the FTP server. 
          client.Connect("myserver", 21, SslSecurityMode.Explicit);
      
          // Authenticate. 
          client.Authenticate("userName", "password");
      
          // Do something here... 
          client.DownloadFile("/my remote file.dat", "my local file");
      
          // Disconnect. 
          client.Disconnect();
      }
      
      /// <summary> 
      /// Returns all issues of the given certificate. 
      /// </summary> 
      /// <param name="status">The certificate verification result.</param> 
      /// <param name="code">The error code.</param> 
      /// <returns>Certificate problems.</returns> 
      private static string GetCertProblem(CertificateVerificationStatus status, int code)
      {
          switch (status)
          {
              case CertificateVerificationStatus.TimeNotValid:
                  return "Server's certificate has expired or is not valid yet.";
      
              case CertificateVerificationStatus.Revoked:
                  return "Server's certificate has been revoked.";
      
              case CertificateVerificationStatus.UnknownCa:
                  return "Server's certificate was issued by an unknown authority.";
      
              case CertificateVerificationStatus.UntrustedRoot:
                  return "Server's certificate was issued by an untrusted authority.";
      
              case CertificateVerificationStatus.IncompleteChain:
                  return "Server's certificate does not chain up to a trusted root authority.";
      
              case CertificateVerificationStatus.Malformed:
                  return "Server's certificate is malformed.";
      
              case CertificateVerificationStatus.CnNotMatch:
                  return "Server hostname does not match the certificate.";
      
              case CertificateVerificationStatus.UnknownError:
                  return string.Format("Error {0:x} encountered while validating server's certificate.", code);
      
              default:
                  return status.ToString();
          }
      }
      
      static void client_CertificateReceived(object sender, ComponentPro.Security.CertificateReceivedEventArgs e)
      {
          X509Certificate2 cert = e.ServerCertificates[0];
      
          CertificateVerificationStatus status = e.Status;
      
          CertificateVerificationStatus[] values = (CertificateVerificationStatus[])Enum.GetValues(typeof(CertificateVerificationStatus));
      
          StringBuilder sbIssues = new StringBuilder();
          for (int i = 0; i < values.Length; i++)
          {
              // Matches the validation status? 
              if ((status & values[i]) == 0)
                  continue;
      
              // The issue is processed. 
              status ^= values[i];
      
              sbIssues.AppendFormat("{0}\r\n", GetCertProblem(values[i], e.ErrorCode));
          }
      
          Console.WriteLine("Issue: " + sbIssues.ToString());
      
          Console.WriteLine("Subject: " + cert.SubjectName.Name);
          Console.WriteLine("Issuer: " + cert.IssuerName.Name);
          Console.WriteLine("Effective Date: " + cert.NotBefore);
          Console.WriteLine("Expiry Date: " + cert.NotAfter);
          Console.ResetColor();
          Console.Write("Do you want to accept this certificate (Add to trusted list, Yes, No) [a,y,n]?");
      
          string response = Console.ReadLine().Trim().ToLower();
      
          // Add certiticate of the issuer CA to the trusted list. 
          if (response == "a")
          {
              e.AddToTrustedRoot = true;
          }
          else if (response == "y")
          {
              e.Accept = true;
          }
      }

      Framework

      .NET Compact Framework.NET Compact Framework

      Supported version: 2.0, 3.5, and 3.9
      Assembly: ComponentPro.Ftp.CF (in ComponentPro.Ftp.CF.dll)

      .NET Framework.NET Framework

      Supported version: 2.0, 3.0, 3.5, 4.0, 4.5.x, 4.6.x and later
      Assembly: ComponentPro.Ftp (in ComponentPro.Ftp.dll)

      Portable Class Library for Windows Phone 8.1 and Windows 8.1 Store AppsPortable Class Library for Windows Phone 8.1 and Windows 8.1 Store Apps

      Supported version: 4.6.x and later
      Assembly: ComponentPro.Ftp.WinPcl (in ComponentPro.Ftp.WinPcl.dll)

      Universal Windows Platform (includes Windows 10 Mobile, Windows 10 Store Apps and Windows 10 IoT)Universal Windows Platform (includes Windows 10 Mobile, Windows 10 Store Apps and Windows 10 IoT)

      Supported version: 4.6.x and later
      Assembly: ComponentPro.Ftp.Uwp (in ComponentPro.Ftp.Uwp.dll)

      Xamarin AndroidXamarin Android

      Supported version: 2.3 and later
      Assembly: ComponentPro.Ftp.Android (in ComponentPro.Ftp.Android.dll)

      Xamarin MacXamarin Mac

      Supported version: 2.0.x and later
      Assembly: ComponentPro.Ftp.Mac (in ComponentPro.Ftp.Mac.dll)

      Xamarin iOSXamarin iOS

      Supported version: 5.1.x and later
      Assembly: ComponentPro.Ftp.iOS (in ComponentPro.Ftp.iOS.dll)

      See Also