ComponentPro UltimateSftp

      AuthenticateAsync(String, KeyboardInteractiveAuthenticationEventHandler) Method

      See AlsoMembers Options: Show AllLanguage Filter: AllSend comments on this topic to ComponentPro
      Asynchronously authenticates the user to the SFTP server.

      Syntax

      public ExtendedAsyncResult AuthenticateAsync(
         string userName, 
         KeyboardInteractiveAuthenticationEventHandler keyboardInteractiveAuthenticationHandler
      )

      Parameters

      userName
      The name of the user to be authenticated.
      keyboardInteractiveAuthenticationHandler
      The keyboard interactive authentication handler.

      Return Value

      An object that references the asynchronous operation.

      Examples

      Shows how to use ConnectAsync, AuthenticateAsync, and DisconnectAsync methods to establish a connection and disconnect asynchronously (Task-based approach).

      using System;
      using System.ComponentModel;
      using System.IO;
      using System.Threading;
      using ComponentPro.Net;
      
      ...
      
      private static async void Main()
      {
          // Create a new class instance. 
          using (Sftp client = new Sftp())
          {
              // Connect to the SFTP server. 
              await client.ConnectAsync("myserver", 22);
      
              // Authenticate. 
              await client.AuthenticateAsync("userName", handler);
      
              // or 
              // await client.AuthenticateAsync("userName", "password", handler); 
       
              client.DownloadFile("/remote file", "c:\\local file");
      
              // ... 
       
              await client.DisconnectAsync();
          }
      }
      
      private static void handler(object sender, KeyboardInteractiveAuthenticationEventArgs e)
      {
          Console.WriteLine("Instructions: " + e.Instructions);
          foreach (KeyboardInteractiveRequest r in e.Requests)
          {
              Console.WriteLine("Prompt: " + r.Prompt);
              Console.Write("Enter response: ");
              r.Response = Console.ReadLine();
          }
      
          Console.Write("Continue or cancel (y/n)?: ");
          if (Console.Read() == 'n')
              e.Cancel = true;
      }

      Shows how to use ConnectAsync, AuthenticateAsync, and DisconnectAsync methods to establish a connection and disconnect asynchronously (Event-based approach).

      using System;
      using System.ComponentModel;
      using System.IO;
      using System.Threading;
      using ComponentPro.Net;
      
      ...
      
      static ManualResetEvent _reset = new ManualResetEvent(false);
      static void Main()
      {
          // Create a new class instance. 
          Sftp client = new Sftp();
      
          client.ConnectCompleted += ClientOnConnectCompleted;
          client.AuthenticateCompleted += ClientOnAuthenticateCompleted;
          client.DisconnectCompleted += ClientOnDisconnectCompleted;
          
          // Connect to the SFTP server. 
          client.ConnectAsync("myserver", 22);
      
          // TODO: Wait until the operation completes 
          _reset.WaitOne();
          // ...
      }
      
      private static void ClientOnDisconnectCompleted(object sender, AsyncCompletedEventArgs e)
      {
          Console.WriteLine("Disconnected");
          _reset.Set();
      }
      
      private static void ClientOnConnectCompleted(object sender, AsyncCompletedEventArgs e)
      {
          Sftp client = (Sftp)sender;
          if (e.Error != null)
          {
              Console.WriteLine("Error: " + e.Error.ToString());
              _reset.Set();
          }
          else 
          {
              // Authenticate. 
              client.AuthenticateAsync("userName", handler);
      
              // or 
              // client.AuthenticateAsync("userName", "password", handler); 
          }
      }
      
      private static void ClientOnAuthenticateCompleted(object sender, AsyncCompletedEventArgs e)
      {
          Sftp client = (Sftp) sender;
          if (e.Error != null)
          {
              Console.WriteLine("Error: " + e.Error.ToString());
              _reset.Set();
          }
          else 
          {
              try 
              {
                  // Do something here... 
                  client.DownloadFile("/my remote file.dat", "my local file");
              }
              catch (Exception ex)
              {
                  Console.WriteLine("Download Error: " + ex.ToString());
              }
      
              client.DisconnectAsync();
          }
      }
      
      static void handler(object sender, KeyboardInteractiveAuthenticationEventArgs e)
      {
          Console.WriteLine("Instructions: " + e.Instructions);
          foreach (KeyboardInteractiveRequest r in e.Requests)
          {
              Console.WriteLine("Prompt: " + r.Prompt);
              Console.Write("Enter response: ");
              r.Response = Console.ReadLine();
          }
      
          Console.Write("Continue or cancel (y/n)?: ");
          if (Console.Read() == 'n')
              e.Cancel = true;
      }

      Framework

      .NET Compact Framework.NET Compact Framework

      Supported version: 2.0, 3.5, and 3.9
      Assembly: ComponentPro.Sftp.CF (in ComponentPro.Sftp.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.Sftp (in ComponentPro.Sftp.dll)

      See Also