ComponentPro UltimateSftp

      Authenticate with a keyboard-interactive authentication

      Language Filter: AllSend comments on this topic to ComponentPro

      Some SSH servers require interactive authentication to authenticate users. In general, they issue a series of question and ask the client to response. The most common question is "Please provide your password" and the client provides a valid password for initiating the verification. To be notified about the questions from the server and provide responses, register a handler of the KeyboardInteractiveAuthentication event.

      using System;
      using ComponentPro.Net;
      
      ...
      
      public void DoConnect()
      {
          // Create a new class instance. 
          Sftp client = new Sftp();
      
          // Connect to the SFTP server. 
          client.Connect("myserver");
      
          // Or you can specify the SFTP port with 
          // client.Connect("myserver", 22); 
       
          // Authenticate. 
          client.Authenticate("userName", "password", KeyboardInteractiveAuthenticationHandler);
      
          // Do something here... 
          client.DownloadFile("/my remote file.dat", "my local file");
      
          // Disconnect. 
          client.Disconnect();
      }
      
      private void KeyboardInteractiveAuthenticationHandler(object sender, KeyboardInteractiveAuthenticationEventArgs e)
      {
          // If we have a request 
          if (e.Requests.Count > 0)
          {
              // If the first request is the string "Password: ". 
              if (string.Compare(e.Requests[0].Prompt, "Password: ", StringComparison.InvariantCultureIgnoreCase) == 0)
              {
                  // We provide password as the response 
                  e.Requests[0].Response = "mypass";
              }
          }
      }