Xceed .NET Libraries Documentation
Welcome to Xceed Data Manipulation Compoents for .NET and .NET Standard / Task-Based Help / FTP capabilities / Making Secure Connections / How To Make An Explicit Secure Connection
In This Topic
How To Make An Explicit Secure Connection
In This Topic

Here is an example on how to make an explicit SSL connection.

static void ExplicitSSLExample()
{
  try
  {
    FtpClient ftp = new FtpClient();
    //ftp.TraceWriter = Console.Out;

    // Subscribe to the CertificateReceived event
    ftp.CertificateReceived += new CertificateReceivedEventHandler( OnCertificateReceived );

    // Connect to the server normally, unencrypted, at the usual ftp port
    ftp.Connect( "localhost", 21 );

    try
    {
      // Pick an authentication method
      AuthenticationMethod authenticationMethod = AuthenticationMethod.Ssl;

      // Pick verification flags. If unsure, pick 'None'.
      VerificationFlags verificationFlags = VerificationFlags.None;

      // Supply a client certificate to submit to the server. This example doesn't use one
      Certificate clientCertificate = null;

      // Decide if the data channel (for file transfers) will be encrypted or not
      DataChannelProtection dataChannelProtection = DataChannelProtection.Private;

      // Authenticate and encrypt the connection
      ftp.Authenticate( authenticationMethod, verificationFlags, clientCertificate, dataChannelProtection );

      // Login. The exchanged information will be encrypted
      ftp.Login( "username", "password" );

      /* Perform your file transfers */
    }
    finally
    {
      // Make sure we always disconnect
      ftp.Disconnect();

      ftp.CertificateReceived -= new CertificateReceivedEventHandler( OnCertificateReceived );
    }
  }
  catch( Exception exception )
  {
    // Output some information about it
    Console.WriteLine( "-->{0}: {1}\n{2}", exception.GetType().Name, exception.Message, exception.StackTrace );

    // Fetch the inner exception
    exception = exception.InnerException;

    // While there is an exception
    while( exception != null )
    {
      // Output some information about it
      Console.WriteLine( "-->Inner exception: {0}: {1}\n{2}", exception.GetType().Name, exception.Message, exception.StackTrace );

      // Fetch the inner exception
      exception = exception.InnerException;
    }
  }
}

static void OnCertificateReceived( object sender, CertificateReceivedEventArgs e )
{
  // The Status argument property tells you if the server certificate was accepted
  // based on the VerificationFlags provided in the call to Connect().
  if( e.Status != VerificationStatus.ValidCertificate )
  {
    Console.WriteLine( "The server certificate is invalid: {0}", e.Status.ToString() );
    Console.WriteLine( e.ServerCertificate.ToString() );

    // You have three choices here:
    //
    //  1) Refuse the certificate by setting e.Action to VerificationAction.Reject,
    //      thus making the authentication fail. This is e.Action's default value
    //      when the server certificate isn't valid.
    //
    //  2) Set e.Flags to less restrictive criterion and ask the library to
    //      validate the certificate again by setting e.Action to
    //      VerificationAction.VerifyAgain.
    //
    //  3) Force the library to accept this certificate by setting e.Action to
    //      VerificationAction.Accept.
    //
    // We'll do #1 or #3, depending on the user's answer.

    Console.WriteLine( "Do you want to accept this certificate anyway? [Y/N]" );

    int answer = Console.Read();
    if( ( answer == 'y' ) || ( answer == 'Y' ) )
    {
      e.Action = VerificationAction.Accept;
    }
  }
  else
  {
    // e.Action's default value is VerificationAction.Accept
    Console.WriteLine( "Valid certificate received from server." );
  }
}
See Also