How to verify SFTP Server's Fingerprint

Some customers have asked us to post an example of how to display and verify the server's fingerprint. It's quite simple if you know that the Sftp class has an event named HostKeyVerifying. You can simply handle this event with the following code:

C#:

client.HostKeyVerifying += client_HostKeyVerifying;


VB.NET

AddHandler client.HostKeyVerifying, AddressOf client_HostKeyVerifying

In the event handler method named client_HostKeyVerifying, you can display the fingerprint of the server to your Console window or Win Form and let user confirm whether he or she accepts that fingerprint. The following code demonstrates how we do that in a Console application:

C#:

Console.WriteLine("Server's fingerprint: " + e.HostKey);
Console.WriteLine("Accept this fingerprint? (y/n): ");
e.Accept = Console.ReadLine().ToLower() == "y";

VB.NET

Console.WriteLine("Server's fingerprint: " & e.HostKey)
Console.WriteLine("Accept this fingerprint? (y/n): ")
e.Accept = Console.ReadLine().ToLower() = "y"