ComponentPro UltimateFtp

      Authenticate with a client certificate

      Language Filter: AllSend comments on this topic to ComponentPro

      When connecting to a secure FTP/SSL server, it may ask the client for a certificate to authenticate the user. To provide a certificate for the authentication, handle the CertificateRequired event, and provide the certificate in the event handler. There are several ways doing that:

      Load certificate from a file

      The following code snippet demonstrates how to load a certificate from a file.

      static void ConnectAndLoadCertFromFile()
      {
          // Create a new instance of the Ftp class. 
          using (Ftp client = new Ftp())
          {
              client.CertificateRequired += client_File_CertificateRequired;
      
              // Connect to the FTP server. 
              client.Connect("myserver", 21, SslSecurityMode.Explicit);
      
              // Authenticate. 
              client.Authenticate("userName", "password");
      
              // ... 
          }
      }
      
      static void client_File_CertificateRequired(object sender, ComponentPro.Security.CertificateRequiredEventArgs e)
      {
          e.Certificates = new X509Certificate2Collection(new X509Certificate2("mycert.pfx", "password"));
      }
      

      Load certificate from user's certificate store

      To provide a certificate in the user's certificate store, use the built-in event handler StoreSearchCertificateRequiredHandler.

      // Create a new instance of the Ftp class. 
      using (Ftp client = new Ftp())
      {
          client.CertificateRequired += ComponentPro.Security.Certificates.CertificateHandlers.StoreSearchCertificateRequiredHandler;
      
          // Connect to the FTP server. 
          client.Connect("myserver", 21, SslSecurityMode.Explicit);
      
          // Authenticate. 
          client.Authenticate("userName", "password");
      
          // ...
      }
      

      Custom certificate handler

      You can also write a custom event handler as shown below: 

      using System;
      using System.Security.Cryptography.X509Certificates;
      using ComponentPro.Net;
      
      ...
      
      static void Main()
      {
          // Create a new instance of the Ftp class. 
          Ftp client = new Ftp();
      
          client.CertificateRequired += client_CertificateRequired;
      
          // 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();
      }
      
      static void client_CertificateRequired(object sender, ComponentPro.Security.CertificateRequiredEventArgs e)
      {
          // Load certificates from the local machine. 
          X509Store my = new X509Store(StoreName.My, StoreLocation.CurrentUser);
          my.Open(OpenFlags.ReadOnly);
      
          // Retrieve a list of available certificates. 
          X509Certificate2Collection certs = my.Certificates;
      
          // If no certificate found, return. 
          if (certs.Count == 0)
          {
              e.Certificates = null;
              return;
          }
      
          // Show all certificates. 
          Console.WriteLine("Select certificate:");
          for (int i = 0; i <= certs.Count; i++)
          {
              if (i == 0)
              {
                  Console.WriteLine(string.Format("{0}. [Nothing, skip this step]", i));
                  continue;
              }
      
              Console.WriteLine(string.Format("{0}. {1}", i, certs[i - 1].SubjectName.Name));
          }
      
          // And ask user to choose an appropriate certificate. 
          while (true)
          {
              Console.Write(string.Format("Select certificate [0 - {0}]: ", certs.Count));
      
              int certIndex;
      
              try 
              {
                  certIndex = int.Parse(Console.ReadLine());
              }
              catch 
              {
                  Console.WriteLine("ERROR: Wrong certificate index input!");
                  continue;
              }
      
              if (certIndex > 0 && certIndex <= certs.Count)
              {
                  e.Certificates = new X509Certificate2Collection(certs[certIndex]);
                  return;
              }
      
              if (certIndex == 0)
                  break;
      
              Console.WriteLine(string.Format("ERROR: You must enter number between 0 and {0}.", certs.Count));
          }
      }