Our Products:   CompleteFTP  edtFTPnet/Free  edtFTPnet/PRO  edtFTPj/Free  edtFTPj/PRO
0 votes
4k views
in .NET FTP by (161k points)
Sometimes you may want to add a host's public key to the known_hosts file in the same way programs like ssh do. Here's how:

1) Load the known_hosts file via SecureFTPConnection.KnownHosts.LoadKnownHosts("known_hosts")

2) Set SecureFTPConnection.ServerValidation = SecureFTPServerValidationType.Callback

3) Implement the ValidatingServer event and set it up. This event is called during validation of the server's public key (for SFTP). It provides an indication of whether the public key has been found in the KnownHostsManager via IsValid, and you can then decide whether to accept the key and add it to the known hosts, or reject it. Here's an example (not compiled but should give you the idea)

AcceptPublicKey is a dialog box to display the fingerprint of the public key and to ask if the user wants it added to known hosts or not.

private void ftp_ValidatingServer(object sender, ServerValidationEventArgs e)
        {
            // if IsValid is true, the key was found in the known hosts list
            if (!e.IsValid && ftp.Protocol.Equals(FileTransferProtocol.SFTP))
            {
                      string fingerprint = e.SSHPublicKey.GetFingerprint();             
                      AcceptPublicKey acceptDlg = new AcceptPublicKey();
                      acceptDlg.fingerprint.Text = fingerprint;
                      DialogResult result = acceptDlg.ShowDialog(this);
                      if (result.Equals(DialogResult.Yes))
                      {
                            // add to known hosts
                            ftp.KnownHostsManager.AddHostKey(ftp.ServerAddress, e.SSHPublicKey);
                            e.IsValid = true;
                      }
               }
        }


and

ftp.ValidatingServer += new ServerValidationHandler(this.ftp_ValidatingServer);

Please log in or register to answer this question.

Categories

...