web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

SFTP connection SSH.NET Authenticate via private key

(0) ShareShare
ReportReport
Posted on by 20

Hi guys,

I am struggling to connect SFTP SSH.net via key based connection.

I am storing key in a variable to use in code.

Below is the my code,

using Renci.SshNet.ScpClient;
using Renci.SshNet;
using System.Net;
using System.Collections.Generic;
using System.Text;

class Test_RunnableClass
{

public static void main(Args _args)
{

File fl;
System.Text.ASCIIEncoding encoding;

new InteropPermission(InteropKind::ClrInterop).assert();


var keyStr = @"-----BEGIN RSA PRIVATE KEY-----xyz-----END RSA PRIVATE KEY-----";

using (var keystrm = new System.IO.MemoryStream(encoding.GetBytes(keyStr)))
{
var privateKey = new PrivateKeyFile(keystrm);

using (var sftp = new Renci.SshNet.SftpClient(host, username, privateKey))
{
sftp.Connect();
sftp.ChangeDirectory(@"/MT940/HDFC Backup/");

var files = sftp.ListDirectory(@"/MT940/HDFC Backup/", null);

var enumerator = files.GetEnumerator();

while (enumerator.MoveNext())
{

var file = enumerator.current;
if ((file.Name != ".") && (file.Name != ".."))
{
info(file.name);
}
}

}
}
}

}

Can you pls help me on this.

I have the same question (0)
  • SandeepNathe Profile Picture
    20 on at

    below is the error above code throwing,

    pastedimage1610958048764v2.png

  • Suggested answer
    nmaenpaa Profile Picture
    101,160 Moderator on at

    Based on the exception, I assume that your "encoding" variable is null. And in your code we can see that you never instantiate it. So, that's your problem.

  • Martin Dráb Profile Picture
    237,795 Most Valuable Professional on at

    By the way, please always use Insert > Insert Code (in the rich-formatting view) to paste source code. It preserves indentation, making code much easier to read. Here is how it looks:

    using Renci.SshNet.ScpClient;
    using Renci.SshNet;
    using System.Net;
    using System.Collections.Generic;
    using System.Text;
    
    class Test_RunnableClass
    {
    	public static void main(Args _args)
    	{
    		File fl;
    		System.Text.ASCIIEncoding encoding;
    
    		new InteropPermission(InteropKind::ClrInterop).assert();
    
    		var keyStr = @"-----BEGIN RSA PRIVATE KEY-----xyz-----END RSA PRIVATE KEY-----";
    
    		using (var keystrm = new System.IO.MemoryStream(encoding.GetBytes(keyStr)))
    		{
    			var privateKey = new PrivateKeyFile(keystrm);
    
    			using (var sftp = new Renci.SshNet.SftpClient(host, username, privateKey))
    			{
    				sftp.Connect();
    				sftp.ChangeDirectory(@"/MT940/HDFC Backup/");
    
    				var files = sftp.ListDirectory(@"/MT940/HDFC Backup/", null);
        			var enumerator = files.GetEnumerator();
    
    				while (enumerator.MoveNext())
    				{
    					var file = enumerator.current;
    					if ((file.Name != ".") && (file.Name != ".."))
    					{
    						info(file.name);
    					}
    				}
    
    			}
    		}
    	}
    
    }

  • SandeepNathe Profile Picture
    20 on at

    Thanks martin for the suggestion, will follow the same in future.

  • SandeepNathe Profile Picture
    20 on at

    Hi Nikoloas,

    Yes, error was due to object not instantiated.

    But still I have another errors.

    My actual requirement to connect SFTP server via key authentication using renci.sshnet.

    Here I am storing key in a variable.

    Can you please guide me on this.

  • nmaenpaa Profile Picture
    101,160 Moderator on at

    Hi Sandeep,

    now you tell us that you have other errors. But you don't share them with us. What kind of help do you expect? And is the question at all related to D365 Finance? 

  • SandeepNathe Profile Picture
    20 on at

    Sorry for incomplete information. I am getting below error,

    pastedimage1610965639705v2.png

    and yes questions is related to D365fo.

  • Suggested answer
    nmaenpaa Profile Picture
    101,160 Moderator on at

    Hi Sandeep,

    actually your problem doesn't seem to be related to D365FO.

    Instead it's about using C# PrivateKeyFile class.

    Just saying this because you could ask help in C# communities that have much wider audience than this Dynamics forum.

    But I suggest to start with a good old web search. I even did it for you and here's one result (also from a forum that I suggest you to search / post): stackoverflow.com/.../renci-sshnet-common-sshexception-invalid-private-key-file-when-loading-ssh-pr

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 663 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 540 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 348 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans