windows sftp server

Tell me how to create an sftp server ?

The task is to test a certain library in which the query query and other parameters (generic) and send a csv file using the ssh key and the library

I created a console application, added libraries, and looked at how to use ssh.net and there are already generated ssh keys.

Question : to test this task, and is it possible to create it on your computer, if anyone can open the veil and enlighten

I need to send a file and find out how the program I'm working on works, I can't find documentation or an easy explanation in Russian, I understand that SFTP ("SSH File Transfer Protocol" or "Secure File Transfer Protocol") but I don't understand how to put the puzzle together, and to be more precise in my question

new SftpConfig()
{
    Host = "?",
    Port = ?,
    UserLogin = "?"
};

The fact that the port is the port that the server listens to is clear, and the example of port 22, also caught my eye, but how to fill in the data to test on your computer and what to enter in the parameters so that it is valid and available locally

// Continuation...

I managed to create a server on one post and try to connect from my own ssh client

I'll leave some useful links here:

Configuring the OpenSSH server on Windows Server 2019

Sshsshd_config file

Installing and configuring an SFTP server (SSH FTP) in Windows based on OpenSSH

I manage to connect via PowerShell using rsa keys, I ran a test application using ssh.net here is the code

var config = new SftpConfig
{
    Host = "IPserver", // 
    Port = 22,
    UserLogin = "LoginName"
};


// step1 create an instance of SftpClient
using var client = new SftpClient(config.Host, config.Port, config.UserLogin);

var pk = new PrivateKeyFile("rsa_key/rsa_test"); 
var keyFiles = new[] { pk };

var methods = new List<AuthenticationMethod>();
methods.Add(new PrivateKeyAuthenticationMethod(config.UserLogin, keyFiles));

var con = new ConnectionInfo(config.Host, config.Port, config.UserLogin, methods.ToArray());

try
{
    // step 2.
    client.Connect();
    using var stream = File.OpenRead("..."); 
    client.UploadFile(stream, "...", true); 
}
catch (Exception exception)
{
    /* client.Disconnect();

     throw new Exception($"{exception.Message} {exception.InnerException}");*/

    Debug.WriteLine("===================>" + $"{exception.Message} {exception.InnerException} {exception.Data} {exception.InnerException}");
}
finally
{
    // step 3.
    client.Disconnect();
}

After the line client. Connect();, it immediately throws an exception Message = " Permission denied (publickey)."

I create a variable con to add rsa keys, but I do not use it, tell me how to change the code so that it works, because using the keys to connect via powershell turned out

var pk = new PrivateKeyFile("rsa_key/id_rsa_test");  
var keyFiles = new[] { pk };

var methods = new List<AuthenticationMethod>();
methods.Add(new PrivateKeyAuthenticationMethod(config.UserLogin, keyFiles));

var con = new ConnectionInfo(config.Host, config.Port, config.UserLogin, methods.ToArray());
using var client = new SftpClient(con);

It turned out to connect... Now I would download the file

Author: MSDN.WhiteKnight, 2020-08-04

1 answers

I made a decision in the form of a response...

var config = new SftpConfig
{
    Host = "IPserver", 
    Port = 22,
    UserLogin = "LoginName"
    PrivateKeyFilePath = "keyRSA_private"
    PrivateKeyFilePassphrase = "pathPhrase"
};

// step1 create an instance of SftpClient with RSA key
var keyFiles = 
    new[]{ new PrivateKeyFile(config.PrivateKeyFilePath, config.PrivateKeyFilePassphrase) };

var methods = new List<AuthenticationMethod>();
methods.Add(new PrivateKeyAuthenticationMethod(config.UserLogin, keyFiles));

var con = new ConnectionInfo(config.Host, config.Port, config.UserLogin, methods.ToArray());
using var client = new SftpClient(con);

try
{
    // step 2.
    client.Connect();
    
    // step bonus => send file
    var localFilePath = "test.txt"
    var remoteFilePath = "testFileForSendingToServer.txt" 
    
    using (Stream fileStream = File.OpenRead(localFilePath))
          client.UploadFile(fileStream, remoteFilePath, true); // true for readonly
}
catch (Exception exception)
{
    logger.LogInformation(exception, 
        $"Failed in uploading file [{localFilePath}] to [{remoteFilePath}]");
}
finally
{
    // step 3.
    client.Disconnect();
}
 1
Author: Dev18, 2020-08-07 08:45:26