ComponentPro UltimateSftp

      AuthenticateAsync(String, String, SecureShellPrivateKey, AsyncCompletedEventHandler, Object) 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 Task AuthenticateAsync(
         string userName, 
         string password, 
         SecureShellPrivateKey privateKey, 
         AsyncCompletedEventHandler callback, 
         object userState = null
      )

      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).
      privateKey
      An RSA or DSA private key for key-based authentication.
      callback
      An optional asynchronous callback to be invoked when this operation completes. This parameter can be null.
      userState
      A user-provided object that identifies this particular asynchronous operation.

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

      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)

      Portable Class Library for Windows Phone 8.1 and Windows 8.1 Store AppsPortable Class Library for Windows Phone 8.1 and Windows 8.1 Store Apps

      Supported version: 4.6.x and later
      Assembly: ComponentPro.Sftp.WinPcl (in ComponentPro.Sftp.WinPcl.dll)

      Universal Windows Platform (includes Windows 10 Mobile, Windows 10 Store Apps and Windows 10 IoT)Universal Windows Platform (includes Windows 10 Mobile, Windows 10 Store Apps and Windows 10 IoT)

      Supported version: 4.6.x and later
      Assembly: ComponentPro.Sftp.Uwp (in ComponentPro.Sftp.Uwp.dll)

      Xamarin AndroidXamarin Android

      Supported version: 2.3 and later
      Assembly: ComponentPro.Sftp.Android (in ComponentPro.Sftp.Android.dll)

      Xamarin MacXamarin Mac

      Supported version: 2.0.x and later
      Assembly: ComponentPro.Sftp.Mac (in ComponentPro.Sftp.Mac.dll)

      Xamarin iOSXamarin iOS

      Supported version: 5.1.x and later
      Assembly: ComponentPro.Sftp.iOS (in ComponentPro.Sftp.iOS.dll)

      See Also