Suppress the use of host key in SFTP or SCP using WinSCP

白昼怎懂夜的黑 提交于 2020-08-05 08:53:58

问题


I am working on an application in which I am downloading files using WinSCP .NET assembly (version 5.5.0.3839)

My code to download file is given below

public bool ReceiveFile(string ftpFilePath,out String downloadedFileName, String DestinationPath)
{
    bool sendingStatus = false;
    downloadedFileName = string.Empty;
    try
    {
        if (sessionOptions != null)
        {
            using (Session session = new Session())
            {
                // Connect
                bool isSessionOpened = OpenSession(session);
                if (isSessionOpened)
                {
                    // Upload files
                    TransferOptions transferOptions = new TransferOptions();
                    transferOptions.TransferMode = TransferMode.Automatic;

                    TransferOperationResult transferResult;
                    transferResult = session.GetFiles(ftpFilePath, DestinationPath, false, transferOptions);
                    // Throw on any error
                    transferResult.Check();

                    // Print results
                    foreach (TransferEventArgs transfer in transferResult.Transfers)
                    {
                        sendingStatus = true;
                        downloadedFileName = transfer.FileName;
                        //We are breaking here because we are assuming that for one import batch setup only one file will be downloaded
                        break;
                    }
                }
            }
        }
    }
    catch (Exception ex)
    {
    }
    return sendingStatus;
}

FTP is working fine, but working with SFTP it requires host key.

When I worked with WinSCP application directly it warns for the host key and user is able to copy host key by clicking on "Copy key" button.

Host key will be copied to clipboard and we can use this key to download files from SFTP or SCP.

There is also an option that user can suppress the use of host key.

I want to suppress the use of host key programmatically.

Let me know if there required any other information.


回答1:


What do you mean by "suppress the use of host key"?

There's no option to suppress use of the host key in WinSCP application. You have an option to accept the host key and remember it, to accept the key without remembering it, and to reject the key (and abandon the connection).
https://winscp.net/eng/docs/ssh_verifying_the_host_key

The WinSCP .NET assembly does not have the first option built-in, as it (by default) does not keep any configuration, so it cannot store the accepted keys anywhere. That's by design. It's your task to set the SessionOptions.SshHostKeyFingerprint. Either:

  • hard-code the host key fingerprint;

  • or load it from any configuration your application have (i.e. possibly from the same configuration you load host name or user name);

  • or implement SSH host key cache (known hosts).

  • or, if you do not care about the host key (meaning you do not care about security at all), you can make WinSCP accept any host key unconditionally using the SessionOption.GiveUpSecurityAndAcceptAnySshHostKey.

    But that's for use in exceptional situations only, where SSH security is unnecessary overhead. Like when both your application and server are within an isolated network.

    See also WinSCP .NET library: Connect to SFTP server without specifying SSH host key fingerprint.


Your code does not show implementation of OpenSession, what is a key piece here, so it's not clear what you are trying to do.



来源:https://stackoverflow.com/questions/24188707/suppress-the-use-of-host-key-in-sftp-or-scp-using-winscp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!