ComponentPro UltimateSftp

      AuthenticateAsync(String, SecureShellPrivateKey) 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, 
         SecureShellPrivateKey privateKey
      )

      Parameters

      userName
      The name of the user to be authenticated.
      privateKey
      An RSA or DSA private key for key-based authentication.

      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;
      
      ...
      
      // 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", new SecureShellPrivateKey("c:\\private key.key", "passphrase"));
      
          // or 
          // await client.AuthenticateAsync("userName", "password", new SecureShellPrivateKey("c:\\private key.key", "passphrase")); 
       
          client.DownloadFile("/remote file", "c:\\local file");
      
          // ... 
       
          await client.DisconnectAsync();
      }

      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", new SecureShellPrivateKey(@"c:\private key.key", "passphrase"));
      
              // or 
              // client.AuthenticateAsync("userName", "password", new SecureShellPrivateKey(@"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