Xceed .NET Libraries Documentation
Welcome to Xceed Data Manipulation Compoents for .NET and .NET Standard / Basic Concepts / Events / CertificateReceived event (FTP-only)

In This Topic
    CertificateReceived event (FTP-only)
    In This Topic

    The CertificateReceived event is raised when an FTP server's certificate has been received and verified.

    Basic steps

    To subscribe to the CertificateReceived event, the following steps must be performed:

    1. Create an instance of the FtpConnection class.

    2. Subscribe to the CertificateReceived event.

    3. If you are using the FtpConnection or FtpClient class in a UI application, assign your form (or any other control that implements the ISynchronizeInvoke interface) to the SynchronizingObject property.

      This will insure that events are triggered on the main UI thread, allowing you to perform UI operations in event handlers. It will also make the component automatically call System.Windows.Forms.Application.DoEvents() during blocking operations

    The following example demonstrates how the CertificateReceived event can be used. The example also uses the SynchronizingObject property. For a description of how certificates and SSL connections work, see Secure FTP (SSL/TLS).

    VB.NET Copy Code

    Imports System
    Imports System.Windows.Forms

    Imports Xceed.FileSystem
    Imports Xceed.Ftp

    Private Shared Sub CertificateReceivedExample(ByVal form As Form)
      Using connection As New FtpConnection("ftp.myplace.com", 990, "ftp_user_name", "ftp_user_name's remote password", AuthenticationMethod.Tls, VerificationFlags.None, Nothing, DataChannelProtection.Private, True)
        ' Subscribe to the CertificateReceived event to consult the certificate sent by the server
        AddHandler connection.CertificateReceived, AddressOf OnCertificateReceived

        ' Make events be redirected to the main UI thread and DoEvents() called in the background
        connection.SynchronizingObject = form

        Dim folder As New FtpFolder(connection)

        For Each item As FileSystemItem In folder.GetItems(True)
          Console.WriteLine(item.FullName)
        Next item
      End Using
    End Sub

    Private Sub OnCertificateReceived(ByVal sender As Object, ByVal e As CertificateReceivedEventArgs)
      ' Here, we should be in the main UI thread since we set the SynchronizingObject property.
      ' We can therefore perform UI operations here without worry.

      ' The Status argument property tells you if the server certificate was accepted
      ' based on the VerificationFlags you provided.
      If e.Status <> VerificationStatus.ValidCertificate Then
        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]")

        Dim answer As Integer = Console.Read()
        If (answer = AscW("y"c)) OrElse (answer = AscW("Y"c)) Then
          e.Action = VerificationAction.Accept
        End If
      Else
        ' e.Action's default value is VerificationAction.Accept
        Console.WriteLine("Valid certificate received from server.")
      End If
    End Sub

    C# Copy Code

    using System;
    using System.Windows.Forms;

    using Xceed.FileSystem;
    using Xceed.Ftp;

    private static void CertificateReceivedExample( Form form )
    {
      using( FtpConnection connection = new FtpConnection( "ftp.myplace.com", 990,
                                                          "ftp_user_name",
                                                          "ftp_user_name's remote password",
                                                          AuthenticationMethod.Tls,
                                                          VerificationFlags.None,
                                                          null,
                                                          DataChannelProtection.Private,
                                                          true ) )
      {
        // Subscribe to the CertificateReceived event to consult the certificate sent by the server
        connection.CertificateReceived += new CertificateReceivedEventHandler( OnCertificateReceived );

        // Make events be redirected to the main UI thread and DoEvents() called in the background
        connection.SynchronizingObject = form;

        FtpFolder folder = new FtpFolder( connection );

        foreach( FileSystemItem item in folder.GetItems( true ) )
        {
          Console.WriteLine( item.FullName );
        }
      }
    }
     
    private void OnCertificateReceived( object sender, CertificateReceivedEventArgs e )
    {
      /* Here, we should be in the main UI thread since we set the SynchronizingObject property.
      * We can therefore perform UI operations here without worry. */

      // The Status argument property tells you if the server certificate was accepted
      // based on the VerificationFlags you provided.
      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." );
      }
    }