.NET integration Exception throw Host key does not match

Advertisement

gvarini
Guest

.NET integration Exception throw Host key does not match

Hi.

I need to send a file throw sftp with private/public keys. Both were generated with putty. Every time i try to open session i get the same exception.

What i dont understand is if this is an error cause by the keys or not. And if in the catch i can make a workaround.

Code:
try
{
//Send Ftp Files - same idea as above - try...catch and try to repeat this code
//if you can't connect the first time, timeout after a certain number of tries.
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Sftp,
HostName = "xxx.xxx.xxx.xxx",
UserName = "user",
PortNumber = 22,
SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:x:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx",

};

var session = new Session();
session.SessionLogPath = ConfigurationManager.AppSettings["BatchPath"] + @"\log.txt";
//session.ExecutablePath = "";
session.Open(sessionOptions); //Attempts to connect to your sFtp site
//Get Ftp File
TransferOptions transferOptions = new TransferOptions();
transferOptions.TransferMode = TransferMode.Binary; //The Transfer Mode -
//<em style="font-size: 9pt;">Automatic, Binary, or Ascii
transferOptions.FilePermissions = null; //Permissions applied to remote files;
//null for default permissions. Can set user,
//Group, or other Read/Write/Execute permissions.
transferOptions.PreserveTimestamp = false; //Set last write time of
//destination file to that of source file - basically change the timestamp
//to match destination and source files.
transferOptions.ResumeSupport.State = TransferResumeSupportState.Off;
//the parameter list is: local Path, Remote Path, Delete source file?, transfer Options
var transferResult = session.PutFiles(filePath, "", false, transferOptions);

//Throw on any error
transferResult.Check();
//Log information and break out if necessary
}
catch (Exception exception)
{
throw exception;
}


Logs:

Host key does not match configured key ssh-rsa 2048 05:3d:0f:62:5d:8d:06:eb:d4:03:1f:49:4f:16:a7:81
. 2015-01-29 09:42:04.868 Asking user:
. 2015-01-29 09:42:04.868 **Continue connecting to an unknown server and add its host key to a cache?**
. 2015-01-29 09:42:04.868
. 2015-01-29 09:42:04.868 The server's host key was not found in the cache. You have no guarantee that the server is the computer you think it is.
. 2015-01-29 09:42:04.868
. 2015-01-29 09:42:04.868 The server's rsa2 key fingerprint is:
. 2015-01-29 09:42:04.868 ssh-rsa 2048 53:a2:4c:2c:22:6a:17:17:37:67:d7:2d:23:7e:86:f1
. 2015-01-29 09:42:04.868
. 2015-01-29 09:42:04.869 If you trust this host, press Yes. To connect without adding host key to the cache, press No. To abandon the connection press Cancel. ()
. 2015-01-29 09:42:04.869 Attempt to close connection due to fatal exception:
* 2015-01-29 09:42:04.869 Host key fingerprint is ssh-rsa 2048 53:a2:4c:2c:22:6a:17:17:37:67:d7:2d:23:7e:86:f1.
* 2015-01-29 09:42:04.869 (Exception) **Host key wasn't verified!**
. 2015-01-29 09:42:04.869 Closing connection.
. 2015-01-29 09:42:04.869 Sending special code: 12

Reply with quote

Advertisement

gvarini
Guest

Thx prikryl for the response.

From the link u gave me, "However, when you connect to a server for the first time, WinSCP has no way of telling whether the host key is the right one or not."

So the first time i try to open session i will always get the exception? In that case i should set GiveUpSecurityAndAcceptAnySshHostKey = TRUE ?

This may happens if on the other sidth (client) i have a proxy making a redirect?

Regards.

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
40,476
Location:
Prague, Czechia

gvarini wrote:

From the link u gave me, "However, when you connect to a server for the first time, WinSCP has no way of telling whether the host key is the right one or not."
That's for GUI.
With .NET assembly, you tell WinSCP explicitly, what host key you expect using the .SshHostKeyFingerprint.

Reply with quote

Advertisement

You can post new topics in this forum