5

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)

Martin Prikryl
  • 147,050
  • 42
  • 335
  • 704
Lahib
  • 1,196
  • 5
  • 29
  • 60

2 Answers2

3

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

Nickolay Olshevsky
  • 12,356
  • 1
  • 28
  • 44
  • do you have ny idea what else i can use, that allows me to set the password of a ppk file ? – Lahib Dec 21 '12 at 10:10
  • 2
    There is free/opensource C# BouncyCastle library, they should support SFTP. Also there is commercial SecureBlackbox library, it definitely supports SFTP and private key passwords. – Nickolay Olshevsky Dec 21 '12 at 10:24
  • WinSCP will support providing private key passphrase in .NET assembly since version 5.6: http://winscp.net/tracker/show_bug.cgi?id=816 – Martin Prikryl Jul 09 '14 at 07:15
1

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.

Martin Prikryl
  • 147,050
  • 42
  • 335
  • 704