Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Files Uploads sometimes but fails most of the time. getting error SSH.NET (Permission denied (publickey)) #620

Closed
DevMusaab opened this issue Feb 5, 2020 · 3 comments
Milestone

Comments

@DevMusaab
Copy link

I am trying to upload file using SFTP. The file gets uploaded sometimes and most the times it throws the error SSH.NET (Permission denied (publickey)) The SFTP server accepts only whitelist IP addresses.

public void SetConnection(string userName, string strRSAKey)
        {
            var authMethods = new List<AuthenticationMethod>();
            authMethods.Add(new PrivateKeyAuthenticationMethod(
                userName, 
                new[] { new PrivateKeyFile(GenerateStream(strRSAKey)) }
                ));

            connectionInfo = new ConnectionInfo(hostName, port, userName, authMethods.ToArray());
        }

        public bool UploadFile(string sourceFilePath, string destinationFilePath)
        {
            if (connectionInfo != null)
            {
                using (var client = new SftpClient(connectionInfo))
                {
                    client.Connect();
                    if (client.IsConnected)
                    {
                        var csvFile = File.Open(sourceFilePath, FileMode.Open);
                        client.UploadFile(csvFile, destinationFilePath, true);
                        csvFile.Dispose();
                    }
                    client.Disconnect();
                    return true;
                }
            }
            return false;
        }

        private Stream GenerateStream(string str)
        {
            var stream = new MemoryStream();
            var writer = new StreamWriter(stream);
            writer.Write(str);
            writer.Flush();
            stream.Position = 0;
            return stream;
        }
@codex-nz
Copy link

I encountered something similar and used the following that seemed to get me further (only to hit another bug/undefined feature):

using (var privateKeyFile = new PrivateKeyFile(_options.PrivateKeyPath))
using (var connectionInfo = new PrivateKeyConnectionInfo(_options.Host, _options.Username, privateKeyFile))
using (_client = new SshClient(connectionInfo))
{ }

NOTE: the use of PrivateKeyConnectionInfo in place of ConnectionInfo.
Hope this helps..

@WojciechNagorski
Copy link
Collaborator

Fixed by #1177 and #1180

@WojciechNagorski
Copy link
Collaborator

Version 2023.0.0 has been published https://www.nuget.org/packages/SSH.NET/2023.0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants