Watch Aspire live streamsDocumentazioneProva Aspire

Connect to an SFTP server with Aspire

Questi contenuti non sono ancora disponibili nella tua lingua.

⭐ Community Toolkit

The Community Toolkit SFTP client registers SSH.NET’s SftpClient in dependency injection. Reference an SFTP resource from your consuming app to provide its connection string. For server configuration, see Set up SFTP with the Aspire integration.

Install the 📦 CommunityToolkit.Aspire.Sftp package in the consuming project:

.NET CLI — Add CommunityToolkit.Aspire.Sftp package
dotnet add package CommunityToolkit.Aspire.Sftp

Call AddSftpClient with the name of the connection from the AppHost. Provide a username and one authentication method:

Program.cs
builder.AddSftpClient(
"sftp",
settings =>
{
settings.Username = "fileuser";
settings.Password = builder.Configuration["Sftp:Password"];
});

The connection name must match the SFTP resource name in the AppHost. Inject SftpClient into a service to transfer files:

FileTransferService.cs
using Renci.SshNet;
public sealed class FileTransferService(SftpClient client)
{
public void Upload(string localPath, string remotePath)
{
if (!client.IsConnected)
{
client.Connect();
}
using var stream = File.OpenRead(localPath);
client.UploadFile(stream, remotePath);
}
}

The integration registers SftpClient as a singleton. Dependency injection disposes it when the application shuts down.

Register keyed clients to connect to multiple SFTP servers:

Program.cs
builder.AddKeyedSftpClient(
"archive",
settings =>
{
settings.Username = "archive-user";
settings.PrivateKeyFile = builder.Configuration["Sftp:ArchivePrivateKeyFile"];
});

Although SftpSettings includes PrivateKeyPassphrase, the client factory creates PrivateKeyFile with only the path. Use an unencrypted private key for SftpClient. The health check uses the passphrase, so it can succeed with an encrypted key even though client creation fails.

Resolve a keyed client using dependency injection:

ArchiveService.cs
using Renci.SshNet;
public sealed class ArchiveService([FromKeyedServices("archive")] SftpClient client)
{
}

For more information, see .NET dependency injection: Keyed services.

The AppHost resource exposes the connection URI sftp://host:port. For a standalone app, set the connection string named sftp:

appsettings.json
{
"ConnectionStrings": {
"sftp": "sftp://localhost:22"
}
}

The client loads SftpSettings from the Aspire:Sftp:Client section. Provide a password or private key path through a secret or deployment configuration provider:

appsettings.json
{
"Aspire": {
"Sftp": {
"Client": {
"ConnectionString": "sftp://localhost:22",
"Username": "fileuser",
"PrivateKeyFile": "/path/to/private/key",
"DisableHealthChecks": false,
"DisableTracing": false
}
}
}
}

The client requires a connection string and username, and it requires either a password or a private key. The health check is enabled by default and verifies that the SFTP server accepts a connection. Set DisableHealthChecks to true to opt out.

Pass an Action<SftpSettings> to configure settings in code:

Program.cs
builder.AddSftpClient(
"sftp",
settings =>
{
settings.Username = "fileuser";
settings.PrivateKeyFile = builder.Configuration["Sftp:PrivateKeyFile"];
settings.DisableTracing = true;
});