Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"No supported authentication methods available" while connecting to SFTP using WinSCP in C#

I'm trying to connect to a SFTP server using WinSCP in C#. This is my code:

SessionOptions sessionOptions = new SessionOptions
{
    Protocol = Protocol.Sftp,
    HostName = "ip",
    PortNumber = portNR,
    UserName = "username",
    Password = "",
    SshHostKeyFingerprint = "fingerPrint", 
    SshPrivateKeyPath = "\\PrivateKey\\PrivateKey.ppk ",
};

using (Session session = new Session())
{
    session.Open(sessionOptions);
}

But when I run the application and it reaches this like

session.Open(sessionOptions);

I get this Error:

Disconnected: No supported authentication methods available (server sent: publickey,gssapi-with-mic)

like image 277
Lahib Avatar asked Oct 05 '22 13:10

Lahib


2 Answers

You should use private key without password or pageant, since WinSCP C# classes don't support setting password for private key.

like image 156
Nickolay Olshevsky Avatar answered Oct 10 '22 02:10

Nickolay Olshevsky


The latest version of WinSCP .NET assembly does support setting passphrase to an encrypted private key.

Use the SessionOptions.PrivateKeyPassphrase.

SessionOptions sessionOptions = new SessionOptions
{
    ...
    SshPrivateKeyPath = "\\PrivateKey\\PrivateKey.ppk",
    SshPrivateKeyPassphrase = "passphrase",
};

See also Automating private key authentication in WinSCP FAQ.

like image 24
Martin Prikryl Avatar answered Oct 10 '22 02:10

Martin Prikryl