Click here to Skip to main content
15,907,497 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was trying to connect to SFTP through code to upload some data. As it has two factor authentication, I need to do some authentication with the Yubikey device before connecting to SFTP. I have written the code to authenticate but I am getting exceptions while authenticating the yubikey device. Is there anything I am doing wrong? Can someone please suggest if there's any better way to do it.

Initially, I was getting error of yubikey pin is more than 8 characters. I have trimmed and captured the first 8 characters to authenticate. Initially, yubikey authentication was successful but was failing in connecting to SFTP and was giving me exception, 'No suitable authentication method found to complete authentication (keyboard-interactive).'" .

But after few attempts, even the yubikey authentication code was failing and giving the exception "There are no retries remaining for a PIN, PUK, or other authentication element".

What I have tried:

C#
try
           {
               YubiKeyDevice yubiKeyDevice =
                   (YubiKeyDevice)YubiKeyDevice.FindAll().FirstOrDefault();
               if (yubiKeyDevice == null)
               {
                   return;
               }

   // Captures yubikey pin here

               using (var pivSession = new PivSession(yubiKeyDevice))
               {
                   yubiKeyPin = yubiKeyPin.Substring
                                (0, Math.Min(yubiKeyPin.Length, 8));

                   byte[] pinBytes = Encoding.UTF8.GetBytes(yubiKeyPin);
                   bool result = pivSession.TryVerifyPin
                                 (pinBytes, out retriesRemaining);

                   if (result)
                   {
                       using (var client = new SftpClient
                             (host, port, username, password))
                       {
                           try
                           {
                               client.Connect();

                               if (client.IsConnected)
                               {
                                   // Now you can proceed with file upload
                                   using (var fileStream =
                                   new FileStream(sourcefile, FileMode.Open))
                                   {
                                       client.UploadFile
                                       (fileStream, destinationpath +
                                        System.IO.Path.GetFileName
                                       (sourcefile));
                                   }
                               }
                           }
                           finally
                           {
                               client.Disconnect();
                           }
                       }
                   }
               }
           }
Posted
Updated 13-Sep-23 3:49am
v3
Comments
Richard MacCutchan 30-Aug-23 12:43pm    
"but i am getting exceptions"
Without the details of the exceptions it is not possible to offer suggestions. Please use the Improve question link above, and add complete details.
Rocky_Bas 30-Aug-23 12:54pm    
Initially i was getting error of yubikey pin is more than 8 characters . I have trimmed and captured first 8 characters to authenticate . Initially yubikey authentication was successful but was failing in connecting to SFTP and was giving me exception 'No suitable authentication method found to complete authentication (keyboard-interactive).'" .

But after few attempts even the yubikey authentication code was failing and giving the exception "There are no retries remaining for a PIN, PUK, or other authentication element"
Richard MacCutchan 30-Aug-23 13:33pm    
These exceptions would appear to be related to the Yubikey system, so you probably need to try their support service.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900