Мне все кажется правильным, но при запуске я получаю сообщение об ошибке:
Ошибка подключения к SFTP-сервер: входная строка имела неправильный формат.
Вот краткий пример моих объектов, которые я настроил (со скрытыми значениями переменных):
Код: Выделить всё
// Configuration setup for SFTP
SFTPConfiguration config = new SFTPConfiguration
{
Host = Environment.GetEnvironmentVariable("HOST"),
Port = 22, // Hardcoded for testing
Username = Environment.GetEnvironmentVariable("USERNAME"),
Password = Environment.GetEnvironmentVariable("PASSWORD"),
PrivateKeyPath = "[FilePath]"
};
SFTPInterface sftpInterface = new SFTPInterface(config);
sftpInterface.TestConnection();
Код: Выделить всё
public SFTPInterface(SFTPConfiguration config)
{
host = config.Host;
port = config.Port;
username = config.Username;
if (!string.IsNullOrEmpty(config.PrivateKeyPath))
{
// Initialize privateKeyFile with config.Password only if the file requires it
try
{
privateKeyFile = !string.IsNullOrEmpty(config.Password) ?
new PrivateKeyFile(config.PrivateKeyPath, config.Password) :
new PrivateKeyFile(config.PrivateKeyPath);
}
catch(IOException)
{
Console.WriteLine($"[SFTPInterface] Unable to read private key file at {config.PrivateKeyPath}");
}
// Instantiate the SftpClient using the privateKeyFile for authentication
sftp = new SftpClient(host, port, username, privateKeyFile);
}
else if (!string.IsNullOrEmpty(config.Password))
{
// Instantiate the SftpClient using password for authentication
sftp = new SftpClient(host, port, username, config.Password);
}
else
{
throw new ArgumentException("[SFTPInterface] Either a password or a private key path must be provided.");
}
- Порт должен быть int. Я пытался жестко закодировать его как int, а также преобразовать в Int32.
- Ключевой файл имеет неправильный формат. Я пробовал импортировать его как .key, .pem и .ppk. Ничего не изменилось (кроме ppk, который выдал ошибку «Неверный ключ»).
Подробнее здесь: https://stackoverflow.com/questions/783 ... -in-a-corr