Attempting to authenticate via username and privatekey only using the current SSH.NET library. I cannot get the password from the user so this is out of the question.
You need some thing like this, it works fine for me. Pay attention while creating new ConnectionInfo object I'm passing only host, port, user and auth method (No password is required); Second difference is that I'm passing single PrivateKeyFile, without empty quotes for 'Phrase' parameter;
public ConnectionInfo GetCertificateBasedConnection()
{
ConnectionInfo connection;
Debug.WriteLine("Trying to create certification based connection...");
using (var stream = new FileStream(ConfigurationHelper.PrivateKeyFilePath, FileMode.Open, FileAccess.Read))
{
var file = new PrivateKeyFile(stream);
var authMethod = new PrivateKeyAuthenticationMethod(ConfigurationHelper.User, file);
connection = new ConnectionInfo(ConfigurationHelper.HostName, ConfigurationHelper.Port, ConfigurationHelper.User, authMethod);
}
Debug.WriteLine("Certification based connection created successfully");
return connection;
}
Your problem here is that you're still using a password, even though it is blank. Remove this line:
new PasswordAuthenticationMethod(username, ""),
This works perfectly for me:
var pk = new PrivateKeyFile(yourkey);
var keyFiles = new[] { pk };
var methods = new List<AuthenticationMethod>();
methods.Add(new PrivateKeyAuthenticationMethod(UserName, keyFiles));
var con = new ConnectionInfo(HostName, Port, UserName, methods.ToArray());
to perform private key authentication, you will also need the passphrase, which together with the private key, will allow authentication.
What is really needed is for RenciSSH to get with the times and write a publickeyauthentication method.