Search code examples
c#sshsftpwinscpwinscp-net

How to get the SSH fingerprint from a server with WinSCP and C#


I want to get the fingerprint when the user is accessing to the server for the first time, and then save that fingerprint (this would not be a problem, I can do this). My problem is that there's supposed to be a method to do that called ScanFingerprint() but that one is not displayed when I'm searching for it.

https://winscp.net/eng/docs/library_session_scanfingerprint#parameters

That's what I've found, but the method does not appear, any idea how to do this?

To be honest, I've not done any code for this, due to the fact that I'm trying to figure out where that method is. But I have this, which I'm planning to use to set the SshHostKeyFingerprint property

TransferOptions = new TransferOptions();
SessionOptions = new SessionOptions();
MySession = new Session();

SessionOptions.HostName = InterfaceConnection.Host;
SessionOptions.UserName = InterfaceConnection.UserID;
SessionOptions.Password = InterfaceConnection.Password;
SessionOptions.PortNumber = InterfaceConnection.Port;

if (string.Compare(protocol.ProtocolUsed, "FTP", true) == 0)                
    SessionOptions.Protocol = WinSCP.Protocol.Ftp;
else if (string.Compare(protocol.ProtocolUsed, "SFTP", true) == 0)
{
    SessionOptions.Protocol = WinSCP.Protocol.Sftp;                    
    SessionOptions.SshHostKeyFingerprint = protocol.FingerPrint;
    SessionOptions.SshPrivateKeyPath = "C:\\TestFiles\\SshKeys\\MyLocalSSHKey";
}

Solution

  • The Session.ScanFingerprint method is available since WinSCP 5.9 only.

    See https://winscp.net/tracker/1394

    For your specific problem on caching the fingerprint, see the Implementing SSH host key cache (known hosts) example.