ComponentPro UltimateSftp

      AuthenticateAsync(String, String, String, String) Method

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

      Syntax

      public ExtendedAsyncResult AuthenticateAsync(
         string userName, 
         string password, 
         string privateKeyFilePath, 
         string passphrase
      )

      Parameters

      userName
      The name of the user to be authenticated.
      password
      The password of the user to be authenticated, or null reference (Nothing in VB.NET).
      privateKeyFilePath
      The path to an RSA or DSA private key for key-based authentication.
      passphrase
      Password used to encrypted the private key, or null if no password needed.

      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())
          {
              client.ConnectCompleted += ClientOnConnectCompleted;
              client.AuthenticateCompleted += ClientOnAuthenticateCompleted;
              client.DisconnectCompleted += ClientOnDisconnectCompleted;
      
              // Connect to the SFTP server. 
              await client.ConnectAsync("myserver", 22, "user state");
      
              // Authenticate. 
              await client.AuthenticateAsync("userName", "password", "c:\\private key.key", "passphrase", "user state");
      
              // ... 
       
              client.DownloadFile("/remote file", "c:\\local file");
      
              await client.DisconnectAsync("user state");
          }
      }
      
      private static void ClientOnDisconnectCompleted(object sender, AsyncCompletedEventArgs e)
      {
          Console.WriteLine("Disconnected. User-state: " + e.UserState);
      }
      
      private static void ClientOnConnectCompleted(object sender, AsyncCompletedEventArgs e)
      {
          if (e.Error != null)
          {
              Console.WriteLine("Error: " + e.Error.ToString());
          }
      
          Console.WriteLine("Connect completed. User-state: " + e.UserState);
      }
      
      private static void ClientOnAuthenticateCompleted(object sender, AsyncCompletedEventArgs e)
      {
          if (e.Error != null)
          {
              Console.WriteLine("Error: " + e.Error.ToString());
          }
      
          Console.WriteLine("Connect completed. User-state: " + e.UserState);
      }

      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", "password", @"c:\private key.key", "passphrase");
          }
      }
      
      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();
          }
      }

      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