无法在.Net Core中使用SSH.NET包连接远程SFTP

问题描述 投票:0回答:3

我想连接到 SFTP 以从那里下载文件。我也可以通过网站和命令提示符(使用 ftp 命令)使用凭据登录 SFTP。现在,我想使用特定的凭据从我的 .Net 代码连接它。

我使用的 SSH.NET 版本为 2020.0.1,.Net Core Runtime 版本为 3.1.18,.Net Core SDK 版本为 3.1.412,操作系统为 Windows (10.0.17763) 64 位。

在尝试通过 .Net Framework 连接 SFTP 时,我可以使用从 NuGet 下载的相同 DLL 版本轻松完成此操作。现在,当我在 .Net Core 项目中使用相同的代码时,它会抛出异常。我已经从 NuGet for .Net Core 下载了相同的软件包版本。

下面是我的代码:

public void Connect()
{
    string host = "xxxx.xx.xxx";
    int port = xx;
    string userid = "xxxx";
    string password = "xxxx";
    
    try
    {
        Renci.SshNet.SftpClient client = new Renci.SshNet.SftpClient(host, port, userid, password);
        if (!client.IsConnected)
        {
            client.ConnectionInfo.Timeout = TimeSpan.FromMinutes(1);
            client.Connect();
            Console.WriteLine("SFTP client connected successfully");
        }
    }
    catch(Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}

我在使用.Net Core时收到的异常消息如下:

通过操作 HashName 属性来访问哈希算法不是 在此平台上支持。相反,您必须实例化其中之一 提供的子类型(例如 HMACSHA1。)

我哪里做错了?任何帮助将不胜感激!!

.net-core sftp ssh.net
3个回答
0
投票

经过大量解决方法后,我找到了解决方案。

SSH.NET 依赖于 SshNet.Security.Cryptography。

SSH.NET(版本 2020.0.1)指的是版本 1.3.0 的 SshNet.Security.Cryptography,而 SSH.NET(版本 2016.1.0)指的是版本 1.2.0 的 SshNet.Security.Cryptography。

I have updated SSH.NET version from 2020.0.1 to 2016.1.0.

可能会出现一些向后兼容性问题。为此,通过降低版本,它正在起作用。

现在,上述代码在 .NET Framework 和 .NET Core 上也可以完美运行。使用 SFTP 连接时不会出现上述错误。


0
投票

这是 SSH.NET 中的一个已知错误:#859。不幸的是,它在 2020.0.2 版本中仍然存在。作为解决方法,您可以使用具体实现覆盖默认的

ConnectionInfo.HmacAlgorithms

var info = new ConnectionInfo(
    "host.com",
    22,
    "user1",
    new PasswordAuthenticationMethod("user1", "secret_password")
);
IDictionary<string, HashInfo> hmacAlgorithms = info.HmacAlgorithms;
hmacAlgorithms["hmac-md5"] = new HashInfo(128, key => new SshNet.Security.Cryptography.HMACMD5(key));
hmacAlgorithms["hmac-md5-96"] = new HashInfo(128, key => new SshNet.Security.Cryptography.HMACMD5(key, 96));
hmacAlgorithms["hmac-sha1"] = new HashInfo(160, key => new SshNet.Security.Cryptography.HMACSHA1(key));
hmacAlgorithms["hmac-sha1-96"] = new HashInfo(160, key => new SshNet.Security.Cryptography.HMACSHA1(key, 96));
hmacAlgorithms["hmac-sha2-256"] = new HashInfo(256, key => new SshNet.Security.Cryptography.HMACSHA256(key));
hmacAlgorithms["hmac-sha2-256-96"] = new HashInfo(256, key => new SshNet.Security.Cryptography.HMACSHA256(key, 96));
hmacAlgorithms["hmac-sha2-512"] = new HashInfo(512, key => new SshNet.Security.Cryptography.HMACSHA512(key));
hmacAlgorithms["hmac-sha2-512-96"] = new HashInfo(512, key => new SshNet.Security.Cryptography.HMACSHA512(key, 96));
hmacAlgorithms["hmac-ripemd160"] = new HashInfo(160, key => new SshNet.Security.Cryptography.HMACRIPEMD160(key));
hmacAlgorithms["[email protected]"] = new HashInfo(160, key => new SshNet.Security.Cryptography.HMACRIPEMD160(key));

0
投票

2023.0.0 版本的 SSH.NET 在 .NET Core 和 .NET 6 及更高版本上运行良好。

https://www.nuget.org/packages/SSH.NET/2023.0.0

© www.soinside.com 2019 - 2024. All rights reserved.