After a bit of research with Gary Bartlett's help, it turned out the server is not actually using "internative" authentication, which means that Sftp.AuthenticationRequest
is not the way to change the password.
The following code can be used instead:
Sftp sftp = new Sftp();
sftp.Connect(serverName, port);
try
{
sftp.Login(userName, password);
}
catch (SftpException error)
{
SshException sshError = error.InnerException as SshException;
if (sshError == null || sshError.Status != SshExceptionStatus.PasswordChangeRequired)
throw;
SshPasswordChangeResult result = sftp.Session.ChangePassword(userName, password, newPassword);
switch (result)
{
case SshPasswordChangeResult.ChangedButNotAuthenticated:
sftp.Session.Authenticate(userName, newPassword);
break;
case SshPasswordChangeResult.Success:
break;
case SshPasswordChangeResult.Failure:
throw new ApplicationException("Unable to change password");
}
sftp.Disconnect();
password = newPassword;
sftp.Connect(serverName, port);
sftp.Login(userName, password);
}