SSH.NET Authenticate via private key only (public key authentication)

前端 未结 3 1342
情话喂你
情话喂你 2021-01-05 08:41

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.

相关标签:
3条回答
  • 2021-01-05 09:20

    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;
        }
    
    0 讨论(0)
  • 2021-01-05 09:25

    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());
    
    0 讨论(0)
  • 2021-01-05 09:36

    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.

    0 讨论(0)
提交回复
热议问题