SFTP using WindSCP.net
In my solution development I have a requirement to perform SFTP File Upload using public key. System should be able to transfer the data using Host, Port, UserName, and Public Key. No Password is required. How can I achieve this using WinSCP .net assembly. I tried the below code. But I am getting Connection TimeOut exception.
public bool TestConnection()
{
try
{
//SessionOptions sessionOptions = BuildSFTPSessionOptions();
// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Sftp,
HostName = this.Host,
PortNumber = this.Port,
UserName = this.UserName,
Password = this.Password,
SshPrivateKeyPath = this.PrivateKeyPath,
FtpMode = FtpMode.Passive,
SshHostKeyFingerprint="ssh-rsa 2048 17:e3:bd:e6:60:f2:75:59:27:44:e3:04:0c:b7:e6:51"
};
using (Session session = new Session())
{
// Connect
session.Open(sessionOptions);
// Move files
if(session.Opened==true)
{
return true;
}
}
return false;
}
catch (Exception ex)
{
throw ex;
}
}
public bool TestConnection()
{
try
{
//SessionOptions sessionOptions = BuildSFTPSessionOptions();
// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Sftp,
HostName = this.Host,
PortNumber = this.Port,
UserName = this.UserName,
Password = this.Password,
SshPrivateKeyPath = this.PrivateKeyPath,
FtpMode = FtpMode.Passive,
SshHostKeyFingerprint="ssh-rsa 2048 17:e3:bd:e6:60:f2:75:59:27:44:e3:04:0c:b7:e6:51"
};
using (Session session = new Session())
{
// Connect
session.Open(sessionOptions);
// Move files
if(session.Opened==true)
{
return true;
}
}
return false;
}
catch (Exception ex)
{
throw ex;
}
}