简体   繁体   中英

How to SFTP authenticate using password or SSH fingerprint WinSCP C# .NET assembly

I'm trying to connect to a server with a SFTP connection, but I'm trying to authenticate with SSH fingerprint, if this is not correct, then should attempt with the SFTP password.

The issue that I'm having is that need both of them to access to the server, that should be different, if is not the SSH fingerprint, then try with the password, but is not working.

There is a way to validate first the fingerprint and if is not correct, validate the user password?

This is what I have:

public string FilesSFTP_FTP()
{          
   TransferOptions TransferOption = new TransferOptions();
   TransferOperationResult TransferResult;
   SessionOptions sessionoptions = new SessionOptions();
   Session session = new Session();

   if (DataFile.sTransportType == "S")
   {
      sessionoptions.Protocol = Protocol.Sftp;
      sessionoptions.PortNumber = 22;
      sessionoptions.SshHostKeyFingerprint = DataFile.sFingerPrint;
   }
   else if (DataFile.sTransportType == "F")
   {
      sessionoptions.Protocol = Protocol.Ftp;
      sessionoptions.PortNumber = 21;
   }

   sessionoptions.HostName = DataFile.sIPAddress;
   sessionoptions.UserName = DataFile.sUserID;
   sessionoptions.Password = DataFile.sPassword;
   TransferOption.TransferMode = TransferMode.Binary;
   TransferOption.PreserveTimestamp = false;
   TransferOption.ResumeSupport.State = TransferResumeSupportState.Off;

   session.Open(sessionoptions);
}

There is another property that it need to be set?

You cannot "authenticate with SSH fingerprint" .


The SessionOptions.SshHostKeyFingerprint is to verify the server's host key . Not to authenticate the user.

To authenticate the user, you need to use the SessionOptions.SshPrivateKeyPath .

See Understanding SSH key pairs to learn the difference.


As for your question. You can set both the SessionOptions.SshPrivateKeyPath and the SessionOptions.Password . WinSCP will first try the private key, and only if that fails, it will fall back to the password.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM