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, ...
Answered

Issue While Connecting to SFTP Using Private Key - Error :'Renci.SshNet.SshNetLoggingConfiguration'

(3) ShareShare
ReportReport
Posted on by 323
Hi experts,
 
I'm trying to connect SFTP server from D365 FO using a username and a private key. The private key was generated using an SSH key.

I'm using C# Code and Renci.SshNet and SSH.NET as a reference in my code. However, while attempting to connect to the server, I'm encountering the following error:
"The type initializer for 'Renci.SshNet.SshNetLoggingConfiguration' threw an exception."

I couldn’t find any relevant blogs or posts addressing this issue. Has anyone faced this error before or have any idea why this might be occurring? Any guidance or solution would be appreciated.
 
Below is my C# code
 
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;
            }
        }
    }
}
Below is my X++ code:
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);
    }
}
 
Categories:
I have the same question (0)
  • Verified answer
    Saalim Ansari Profile Picture
    666 on at

    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.

    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

  • Verified answer
    Martin Dráb Profile Picture
    237,801 Most Valuable Professional on at
    Does your code works when executed directly in a C# console application or so? If not, then we don't need to complicate the discussion by adding F&O there at all, because it's not really related to it.
     
    If it works there but in F&O, you know that the code is find and you need to look for the problem somewhere else.
     
    The usage of a path to use user profile looks suspicious. Are you sure that the service account (running the web server) has permissions to access this file? You won't be able to use such an approach in the real solution anyway, so maybe you could already design something more suitable for cloud.
     
    Also, your error handling logic may hide important information about the exception: the type, the stack trace, inner exceptions and all properties except of Message. I suggest you stop handling the exceptions for now or you debug the C# code to see the original exception.
  • Saran S Profile Picture
    323 on at
     
    Thanks for the info.
     
    I tried downgrading the SSH.NET version to 2016.1.0 version,
     
    here i got error like below in the same code
     
     
    so I changed my code to this
     
    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.";
             }
         }
    here the error is not return, but while running the code got the below error

    Method not found: 'Void Renci.SshNet.PrivateKeyAuthenticationMethod..ctor(System.String, Renci.SshNet.PrivateKeyFile[])'.
     
    Have any suggestion.
  • Saran S Profile Picture
    323 on at
    Hi @Martin,
     
    Thanks for the info.
     
    1. Code works when executed directly in a C# console application.
     
    2. I tried to connect with SFTP server using FileZilla it works fine and also Command prompt too.
     
    3. I get the above error while debugging. (For your reference)
  • Martin Dráb Profile Picture
    237,801 Most Valuable Professional on at
    You need to develop against the version used by F&O. Downloading a different version and developing against it (without using it in F&O) means that, for example, you may be using methods in code that do not exist at runtime. The assembly then either fails to load or it'll end up with a runtime exception (as in your case).
     
    Make sure that version you use for development match the version you've installed to F&O. I think that Renci.SshNet isn't included by default, therefore I'm assuming that you've installed it by yourself.

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 664 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

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

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 303 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans