Announcements
No record found.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Renci.SshNet; using Renci.SshNet.Sftp; using System.IO; using System.Net; namespace SFTPConnection { public class SFTPConnectionAndUploadFile { public static SftpClient ConnectFTP( string host, string userName, string privateKeyPath, string KeyFilePassCode, int port = 22) { SftpClient sftpClient = (SftpClient)null; try { PrivateKeyFile privateKeyFile = new PrivateKeyFile(privateKeyPath, KeyFilePassCode); sftpClient = new SftpClient(new ConnectionInfo(host, port, userName, new AuthenticationMethod[1] //Got error in this line { (AuthenticationMethod) new PrivateKeyAuthenticationMethod(userName, new IPrivateKeySource[1] { (IPrivateKeySource) privateKeyFile }) })); ((BaseClient)sftpClient).Connect(); return sftpClient; } catch (Exception ex) { Console.WriteLine("An error occurred: " + ex.Message); ((BaseClient)sftpClient)?.Dispose(); return (SftpClient)null; } } public static SftpClient ConnectFTP( string host, string userName, Stream privateKey, string KeyFilePassCode, int port = 22) { SftpClient sftpClient = (SftpClient)null; try { //PrivateKeyFile privateKeyFile = new PrivateKeyFile(privateKeyPath, KeyFilePassCode); PrivateKeyFile privateKeyFile = new PrivateKeyFile(privateKey, KeyFilePassCode); sftpClient = new SftpClient(new ConnectionInfo(host, port, userName, new AuthenticationMethod[1] //Got error in this line { (AuthenticationMethod) new PrivateKeyAuthenticationMethod(userName, new IPrivateKeySource[1] { (IPrivateKeySource) privateKeyFile }) })); ((BaseClient)sftpClient).Connect(); return sftpClient; } catch (Exception ex) { Console.WriteLine("An error occurred: " + ex.Message); ((BaseClient)sftpClient)?.Dispose(); return (SftpClient)null; } } } }
using SFTPConnection; using Renci.SshNet; class EncryptTest { public static void main(Args args) { System.Exception ex; Renci.SshNet.SftpClient sftpClient; str connectionStatus; sftpClient = SFTPConnection.SFTPConnectionAndUploadFile::ConnectFTP('100.100.100.10', 'xxyyzz', "C:\Users\Admin\id_rsa", 'XXYYZZ', 22); sftpClient.Connect(); if (sftpClient.IsConnected) connectionStatus = 'Connected'; else connectionStatus = 'Not Connected'; Info(connectionStatus); } }
Hello, I faced a similar issue when using Renci.SshNet with SSH key authentication in D365 FO. The error often comes from using an incompatible or newer version of SSH.NET. Try downgrading to version 2016.1.0, which is stable and works well with D365FO's supported .NET versions.
Renci.SshNet
Also, here's a great step-by-step blog that shows how to connect and upload files using either SSH key or username/password from D365 FO via X++ and C#: 🔗 https://harinmpatel.hashnode.dev/upload-a-file-on-sftp-server-using-ssh-key-or-usernamepassword-in-d365-using-x-part-1
Hope it helps! Saalim
List<AuthenticationMethod> authenticationMethodList; PrivateKeyFile privateKeyFile = new PrivateKeyFile(privateKeyPath, passphrase); authenticationMethodList = new List<AuthenticationMethod>() { (AuthenticationMethod) new PrivateKeyAuthenticationMethod(username, new PrivateKeyFile[1] { privateKeyFile }) }; string str; try { using (SftpClient sftpClient = new SftpClient(new ConnectionInfo(host, port, username, authenticationMethodList.ToArray()))) { sftpClient.Connect(); str = "SFTP Server connected."; } }
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.
Congratulations to our 2026 Super Stars!
We are thrilled to have these Champions in our Community!
These are the community rock stars!
Stay up to date on forum activity by subscribing.
Giorgio Bonacorsi 616
André Arnaud de Cal... 460 Super User 2026 Season 1
Syed Haris Shah 331 Super User 2026 Season 1