Download fails using .NET Assembly
Software: WinSCP 5.1.0
Transfer Protocol: SFTP-3
GUI Style: Explorer
FTP Server Software: freeSSHd 1.2.4
OS: Windows 7
Subject: Download fails using .NET Assembly
For testing, I'm running freeSSHd and WinSCP on my machine with FULL admin permissions.
I can successfully download files from freeSSHd using the WinSCP GUI.
But when I try the same using automation in my C# program, I get the error:
"Can't get attributes of file 'C:\SFTPHome\hostedfiles'."
with inner exception: "Permission denied. Error code: 3 Error message from server: Permission denied Request code: 7".
My Code:
private void DownloadFiles()
{
try
{
// Setup session options...
SessionOptions sessionOptions = new SessionOptions
{
SshPrivateKeyPath = @"C:\Users\tkargel\Documents\Visual Studio 2010\Projects\Resolute\ResoportDownload\sftpkey.ppk",
PortNumber = 22,
Protocol = Protocol.Sftp,
HostName = "LT-CND0411CDW",
UserName = "sftpuser2",
SshHostKeyFingerprint = "ssh-rsa 1024 65:2b:97:1d:2c:24:30:5a:ec:b2:33:85:4d:df:17:74"
};
using (Session session = new Session())
{
// Connect
session.SessionLogPath = ("C:\\SFTPHome\\freesshd.log");
session.Open(sessionOptions);
// Download files
TransferOptions transferOptions = new TransferOptions();
transferOptions.TransferMode = TransferMode.Binary;
TransferOperationResult transferResult;
transferResult = session.GetFiles(GlobalVar.PathFilesHosted, GlobalVar.PathFilesDownloaded, false, transferOptions);
// Throw on any error
transferResult.Check();
// Print results
foreach (TransferEventArgs transfer in transferResult.Transfers)
{
Console.WriteLine("Upload of {0} succeeded", transfer.FileName);
}
}
}
catch (Exception ex)
{
Console.WriteLine("Error: {0}", ex);
throw;
}
}
Comparative logs:
// Download file using WinSCP GUI ///////////////////////////////////////////////////
09-28-2012 16:27:22 HOST LT-CND0411CDW.ResoluteEnergy.com SSH connection attempt.
09-28-2012 16:27:23 HOST LT-CND0411CDW.ResoluteEnergy.com SSH sftpuser2 successfully logged on using public key.
09-28-2012 16:27:23 SFTP service granted to user sftpuser2.
09-28-2012 16:27:23 HOST LT-CND0411CDW.ResoluteEnergy.com sftpuser2 is listing /hostedfiles (C:\SFTPHome\hostedfiles)
09-28-2012 16:28:31 HOST LT-CND0411CDW.ResoluteEnergy.com sftpuser2 is downloading /hostedfiles/FTP1.txt (C:\SFTPHome\hostedfiles\FTP1.txt)
// Download file using .NET Assembly in C# program //////////////////////////////////
. 2012-09-28 16:36:04.993 --------------------------------------------------------------------------
. 2012-09-28 16:36:04.993 WinSCP Version 5.1.0 (Build 2625) (OS 6.1.7601 Service Pack 1)
. 2012-09-28 16:36:04.993 Configuration: nul
. 2012-09-28 16:36:04.993 Local account: RESOLUTEENERGY\tkargel
. 2012-09-28 16:36:04.994 Working directory: C:\Users\tkargel\Documents\Visual Studio 2010\Projects\Resolute\ResoportDownload\ResoportConsole\ResoportConsole\bin
\Debug
. 2012-09-28 16:36:04.994 Command-line: "C:\Program Files (x86)\WinSCP\winscp.exe" /xmllog="C:\Users\tkargel\AppData\Local\Temp\tmp9351.tmp" /xmlgroups
/nointeractiveinput /dotnet=510 /ini=nul /log="C:\SFTPHome\freesshd.log" /console /consoleinstance=_4736_113
. 2012-09-28 16:36:04.994 Login time: Friday, September 28, 2012 4:36:04 PM
. 2012-09-28 16:36:04.994 --------------------------------------------------------------------------
. 2012-09-28 16:36:04.994 Session name: sftpuser2@LT-CND0411CDW (Ad-Hoc session)
. 2012-09-28 16:36:04.994 Host name: LT-CND0411CDW (Port: 22)
. 2012-09-28 16:36:04.994 User name: sftpuser2 (Password: No, Key file: Yes)
. 2012-09-28 16:36:04.994 Tunnel: No
. 2012-09-28 16:36:04.994 Transfer Protocol: SFTP
. 2012-09-28 16:36:04.994 Ping type: -, Ping interval: 30 sec; Timeout: 15 sec
. 2012-09-28 16:36:04.994 Proxy: none
. 2012-09-28 16:36:04.994 SSH protocol version: 2; Compression: No
. 2012-09-28 16:36:04.994 Bypass authentication: No
. 2012-09-28 16:36:04.994 Try agent: Yes; Agent forwarding: No; TIS/CryptoCard: No; KI: Yes; GSSAPI: No
. 2012-09-28 16:36:04.994 Ciphers: aes,blowfish,3des,WARN,arcfour,des; Ssh2DES: No
. 2012-09-28 16:36:04.994 SSH Bugs: A,A,A,A,A,A,A,A,A,A
. 2012-09-28 16:36:04.994 SFTP Bugs: A,A
. 2012-09-28 16:36:04.994 Return code variable: Autodetect; Lookup user groups: A
. 2012-09-28 16:36:04.994 Shell: default
. 2012-09-28 16:36:04.994 EOL: 0, UTF: 2
. 2012-09-28 16:36:04.994 Clear aliases: Yes, Unset nat.vars: Yes, Resolve symlinks: Yes
. 2012-09-28 16:36:04.994 LS: ls -la, Ign LS warn: Yes, Scp1 Comp: No
. 2012-09-28 16:36:04.994 Local directory: default, Remote directory: home, Update: Yes, Cache: Yes
. 2012-09-28 16:36:04.994 Cache directory changes: Yes, Permanent: Yes
. 2012-09-28 16:36:04.994 DST mode: 1
. 2012-09-28 16:36:04.994 --------------------------------------------------------------------------
. 2012-09-28 16:36:04.994 Looking up host "LT-CND0411CDW"
. 2012-09-28 16:36:04.998 Connecting to fe80::60fb:1baf:338f:5ae9%13 port 22
. 2012-09-28 16:36:06.182 Failed to connect to fe80::60fb:1baf:338f:5ae9%13: Network error: Connection refused
. 2012-09-28 16:36:06.182 Connecting to fe80::60fb:1baf:338f:5ae9%13 port 22
09-28-2012 16:36:06 HOST LT-CND0411CDW.ResoluteEnergy.com SSH connection attempt.
. 2012-09-28 16:36:06.182 Server version: SSH-2.0-WeOnlyDo 2.1.3
. 2012-09-28 16:36:06.182 Using SSH protocol version 2
. 2012-09-28 16:36:06.182 We claim version: SSH-2.0-WinSCP_release_5.1
. 2012-09-28 16:36:06.182 Using Diffie-Hellman with standard group "group14"
. 2012-09-28 16:36:06.182 Doing Diffie-Hellman key exchange with hash SHA-1
. 2012-09-28 16:36:06.385 Verifying host key rsa2 ...
. 2012-09-28 16:36:06.385 Host key matches configured key
. 2012-09-28 16:36:06.385 Host key fingerprint is:
. 2012-09-28 16:36:06.385 ssh-rsa 1024 65:2b:97:1d:2c:24:30:5a:ec:b2:33:85:4d:df:17:74
. 2012-09-28 16:36:06.385 Initialised AES-256 CBC client->server encryption
. 2012-09-28 16:36:06.385 Initialised HMAC-SHA1 client->server MAC algorithm
. 2012-09-28 16:36:06.385 Initialised AES-256 CBC server->client encryption
. 2012-09-28 16:36:06.385 Initialised HMAC-SHA1 server->client MAC algorithm
. 2012-09-28 16:36:06.385 Reading private key file "C:\Users\tkargel\Documents\Visual Studio 2010\Projects\Resolute\ResoportDownload\sftpkey.ppk"
! 2012-09-28 16:36:06.385 Using username "sftpuser2".
. 2012-09-28 16:36:06.385 Offered public key
. 2012-09-28 16:36:06.385 Offer of public key accepted
! 2012-09-28 16:36:06.385 Authenticating with public key "rsa-key-20120928"
. 2012-09-28 16:36:06.401 Sent public key signature
09-28-2012 16:36:06 HOST LT-CND0411CDW.ResoluteEnergy.com SSH sftpuser2 successfully logged on using public key.
. 2012-09-28 16:36:06.401 Access granted
. 2012-09-28 16:36:06.401 Opened channel for session
09-28-2012 16:36:06 SFTP service granted to user sftpuser2.
. 2012-09-28 16:36:06.416 Started a shell/command
. 2012-09-28 16:36:06.416 --------------------------------------------------------------------------
. 2012-09-28 16:36:06.416 Using SFTP protocol.
. 2012-09-28 16:36:06.416 Doing startup conversation with host.
> 2012-09-28 16:36:06.416 Type: SSH_FXP_INIT, Size: 5, Number: -1
< 2012-09-28 16:36:06.416 Type: SSH_FXP_VERSION, Size: 5, Number: -1
. 2012-09-28 16:36:06.416 SFTP version 3 negotiated.
. 2012-09-28 16:36:06.416 We believe the server has signed timestamps bug
. 2012-09-28 16:36:06.416 We will use UTF-8 strings for status messages only
. 2012-09-28 16:36:06.416 Getting current directory name.
. 2012-09-28 16:36:06.416 Getting real path for '.'
> 2012-09-28 16:36:06.416 Type: SSH_FXP_REALPATH, Size: 10, Number: 16
< 2012-09-28 16:36:06.416 Type: SSH_FXP_NAME, Size: 23, Number: 16
. 2012-09-28 16:36:06.416 Real path is '/'
. 2012-09-28 16:36:06.416 Startup conversation with host finished.
< 2012-09-28 16:36:06.416 Script: Active session: [1] sftpuser2@LT-CND0411CDW
> 2012-09-28 16:36:07.118 Script: get -nopermissions -preservetime -transfer="binary" -- "C:\SFTPHome\hostedfiles" "C:\Users\tkargel\Documents\Visual Studio
2010\Projects\Resolute\ResoportDownload\ResoportData\FilesDownloaded"
. 2012-09-28 16:36:07.118 Listing file "C:\SFTPHome\hostedfiles".
> 2012-09-28 16:36:07.118 Type: SSH_FXP_LSTAT, Size: 33, Number: 263
< 2012-09-28 16:36:07.118 Type: SSH_FXP_STATUS, Size: 34, Number: 263
< 2012-09-28 16:36:07.118 Status code: 3, Message: 263, Server: Permission denied, Language:
< 2012-09-28 16:36:07.118 Script: Can't get attributes of file 'C:\SFTPHome\hostedfiles'.
< 2012-09-28 16:36:07.118 Script: Permission denied.
< 2012-09-28 16:36:07.118 Error code: 3
< 2012-09-28 16:36:07.118 Error message from server: Permission denied
< 2012-09-28 16:36:07.118 Request code: 7
. 2012-09-28 16:36:07.118 Script: Failed
> 2012-09-28 16:36:09.463 Script: exit
. 2012-09-28 16:36:09.464 Script: Exit code: 1
. 2012-09-28 16:36:09.464 Closing connection.
. 2012-09-28 16:36:09.464 Sending special code: 12
. 2012-09-28 16:36:09.464 Sent EOF message
09-28-2012 16:36:09 HOST LT-CND0411CDW.ResoluteEnergy.com SSH sftpuser2 disconnected.
Transfer Protocol: SFTP-3
GUI Style: Explorer
FTP Server Software: freeSSHd 1.2.4
OS: Windows 7
Subject: Download fails using .NET Assembly
For testing, I'm running freeSSHd and WinSCP on my machine with FULL admin permissions.
I can successfully download files from freeSSHd using the WinSCP GUI.
But when I try the same using automation in my C# program, I get the error:
"Can't get attributes of file 'C:\SFTPHome\hostedfiles'."
with inner exception: "Permission denied. Error code: 3 Error message from server: Permission denied Request code: 7".
My Code:
private void DownloadFiles()
{
try
{
// Setup session options...
SessionOptions sessionOptions = new SessionOptions
{
SshPrivateKeyPath = @"C:\Users\tkargel\Documents\Visual Studio 2010\Projects\Resolute\ResoportDownload\sftpkey.ppk",
PortNumber = 22,
Protocol = Protocol.Sftp,
HostName = "LT-CND0411CDW",
UserName = "sftpuser2",
SshHostKeyFingerprint = "ssh-rsa 1024 65:2b:97:1d:2c:24:30:5a:ec:b2:33:85:4d:df:17:74"
};
using (Session session = new Session())
{
// Connect
session.SessionLogPath = ("C:\\SFTPHome\\freesshd.log");
session.Open(sessionOptions);
// Download files
TransferOptions transferOptions = new TransferOptions();
transferOptions.TransferMode = TransferMode.Binary;
TransferOperationResult transferResult;
transferResult = session.GetFiles(GlobalVar.PathFilesHosted, GlobalVar.PathFilesDownloaded, false, transferOptions);
// Throw on any error
transferResult.Check();
// Print results
foreach (TransferEventArgs transfer in transferResult.Transfers)
{
Console.WriteLine("Upload of {0} succeeded", transfer.FileName);
}
}
}
catch (Exception ex)
{
Console.WriteLine("Error: {0}", ex);
throw;
}
}
Comparative logs:
// Download file using WinSCP GUI ///////////////////////////////////////////////////
09-28-2012 16:27:22 HOST LT-CND0411CDW.ResoluteEnergy.com SSH connection attempt.
09-28-2012 16:27:23 HOST LT-CND0411CDW.ResoluteEnergy.com SSH sftpuser2 successfully logged on using public key.
09-28-2012 16:27:23 SFTP service granted to user sftpuser2.
09-28-2012 16:27:23 HOST LT-CND0411CDW.ResoluteEnergy.com sftpuser2 is listing /hostedfiles (C:\SFTPHome\hostedfiles)
09-28-2012 16:28:31 HOST LT-CND0411CDW.ResoluteEnergy.com sftpuser2 is downloading /hostedfiles/FTP1.txt (C:\SFTPHome\hostedfiles\FTP1.txt)
// Download file using .NET Assembly in C# program //////////////////////////////////
. 2012-09-28 16:36:04.993 --------------------------------------------------------------------------
. 2012-09-28 16:36:04.993 WinSCP Version 5.1.0 (Build 2625) (OS 6.1.7601 Service Pack 1)
. 2012-09-28 16:36:04.993 Configuration: nul
. 2012-09-28 16:36:04.993 Local account: RESOLUTEENERGY\tkargel
. 2012-09-28 16:36:04.994 Working directory: C:\Users\tkargel\Documents\Visual Studio 2010\Projects\Resolute\ResoportDownload\ResoportConsole\ResoportConsole\bin
\Debug
. 2012-09-28 16:36:04.994 Command-line: "C:\Program Files (x86)\WinSCP\winscp.exe" /xmllog="C:\Users\tkargel\AppData\Local\Temp\tmp9351.tmp" /xmlgroups
/nointeractiveinput /dotnet=510 /ini=nul /log="C:\SFTPHome\freesshd.log" /console /consoleinstance=_4736_113
. 2012-09-28 16:36:04.994 Login time: Friday, September 28, 2012 4:36:04 PM
. 2012-09-28 16:36:04.994 --------------------------------------------------------------------------
. 2012-09-28 16:36:04.994 Session name: sftpuser2@LT-CND0411CDW (Ad-Hoc session)
. 2012-09-28 16:36:04.994 Host name: LT-CND0411CDW (Port: 22)
. 2012-09-28 16:36:04.994 User name: sftpuser2 (Password: No, Key file: Yes)
. 2012-09-28 16:36:04.994 Tunnel: No
. 2012-09-28 16:36:04.994 Transfer Protocol: SFTP
. 2012-09-28 16:36:04.994 Ping type: -, Ping interval: 30 sec; Timeout: 15 sec
. 2012-09-28 16:36:04.994 Proxy: none
. 2012-09-28 16:36:04.994 SSH protocol version: 2; Compression: No
. 2012-09-28 16:36:04.994 Bypass authentication: No
. 2012-09-28 16:36:04.994 Try agent: Yes; Agent forwarding: No; TIS/CryptoCard: No; KI: Yes; GSSAPI: No
. 2012-09-28 16:36:04.994 Ciphers: aes,blowfish,3des,WARN,arcfour,des; Ssh2DES: No
. 2012-09-28 16:36:04.994 SSH Bugs: A,A,A,A,A,A,A,A,A,A
. 2012-09-28 16:36:04.994 SFTP Bugs: A,A
. 2012-09-28 16:36:04.994 Return code variable: Autodetect; Lookup user groups: A
. 2012-09-28 16:36:04.994 Shell: default
. 2012-09-28 16:36:04.994 EOL: 0, UTF: 2
. 2012-09-28 16:36:04.994 Clear aliases: Yes, Unset nat.vars: Yes, Resolve symlinks: Yes
. 2012-09-28 16:36:04.994 LS: ls -la, Ign LS warn: Yes, Scp1 Comp: No
. 2012-09-28 16:36:04.994 Local directory: default, Remote directory: home, Update: Yes, Cache: Yes
. 2012-09-28 16:36:04.994 Cache directory changes: Yes, Permanent: Yes
. 2012-09-28 16:36:04.994 DST mode: 1
. 2012-09-28 16:36:04.994 --------------------------------------------------------------------------
. 2012-09-28 16:36:04.994 Looking up host "LT-CND0411CDW"
. 2012-09-28 16:36:04.998 Connecting to fe80::60fb:1baf:338f:5ae9%13 port 22
. 2012-09-28 16:36:06.182 Failed to connect to fe80::60fb:1baf:338f:5ae9%13: Network error: Connection refused
. 2012-09-28 16:36:06.182 Connecting to fe80::60fb:1baf:338f:5ae9%13 port 22
09-28-2012 16:36:06 HOST LT-CND0411CDW.ResoluteEnergy.com SSH connection attempt.
. 2012-09-28 16:36:06.182 Server version: SSH-2.0-WeOnlyDo 2.1.3
. 2012-09-28 16:36:06.182 Using SSH protocol version 2
. 2012-09-28 16:36:06.182 We claim version: SSH-2.0-WinSCP_release_5.1
. 2012-09-28 16:36:06.182 Using Diffie-Hellman with standard group "group14"
. 2012-09-28 16:36:06.182 Doing Diffie-Hellman key exchange with hash SHA-1
. 2012-09-28 16:36:06.385 Verifying host key rsa2 ...
. 2012-09-28 16:36:06.385 Host key matches configured key
. 2012-09-28 16:36:06.385 Host key fingerprint is:
. 2012-09-28 16:36:06.385 ssh-rsa 1024 65:2b:97:1d:2c:24:30:5a:ec:b2:33:85:4d:df:17:74
. 2012-09-28 16:36:06.385 Initialised AES-256 CBC client->server encryption
. 2012-09-28 16:36:06.385 Initialised HMAC-SHA1 client->server MAC algorithm
. 2012-09-28 16:36:06.385 Initialised AES-256 CBC server->client encryption
. 2012-09-28 16:36:06.385 Initialised HMAC-SHA1 server->client MAC algorithm
. 2012-09-28 16:36:06.385 Reading private key file "C:\Users\tkargel\Documents\Visual Studio 2010\Projects\Resolute\ResoportDownload\sftpkey.ppk"
! 2012-09-28 16:36:06.385 Using username "sftpuser2".
. 2012-09-28 16:36:06.385 Offered public key
. 2012-09-28 16:36:06.385 Offer of public key accepted
! 2012-09-28 16:36:06.385 Authenticating with public key "rsa-key-20120928"
. 2012-09-28 16:36:06.401 Sent public key signature
09-28-2012 16:36:06 HOST LT-CND0411CDW.ResoluteEnergy.com SSH sftpuser2 successfully logged on using public key.
. 2012-09-28 16:36:06.401 Access granted
. 2012-09-28 16:36:06.401 Opened channel for session
09-28-2012 16:36:06 SFTP service granted to user sftpuser2.
. 2012-09-28 16:36:06.416 Started a shell/command
. 2012-09-28 16:36:06.416 --------------------------------------------------------------------------
. 2012-09-28 16:36:06.416 Using SFTP protocol.
. 2012-09-28 16:36:06.416 Doing startup conversation with host.
> 2012-09-28 16:36:06.416 Type: SSH_FXP_INIT, Size: 5, Number: -1
< 2012-09-28 16:36:06.416 Type: SSH_FXP_VERSION, Size: 5, Number: -1
. 2012-09-28 16:36:06.416 SFTP version 3 negotiated.
. 2012-09-28 16:36:06.416 We believe the server has signed timestamps bug
. 2012-09-28 16:36:06.416 We will use UTF-8 strings for status messages only
. 2012-09-28 16:36:06.416 Getting current directory name.
. 2012-09-28 16:36:06.416 Getting real path for '.'
> 2012-09-28 16:36:06.416 Type: SSH_FXP_REALPATH, Size: 10, Number: 16
< 2012-09-28 16:36:06.416 Type: SSH_FXP_NAME, Size: 23, Number: 16
. 2012-09-28 16:36:06.416 Real path is '/'
. 2012-09-28 16:36:06.416 Startup conversation with host finished.
< 2012-09-28 16:36:06.416 Script: Active session: [1] sftpuser2@LT-CND0411CDW
> 2012-09-28 16:36:07.118 Script: get -nopermissions -preservetime -transfer="binary" -- "C:\SFTPHome\hostedfiles" "C:\Users\tkargel\Documents\Visual Studio
2010\Projects\Resolute\ResoportDownload\ResoportData\FilesDownloaded"
. 2012-09-28 16:36:07.118 Listing file "C:\SFTPHome\hostedfiles".
> 2012-09-28 16:36:07.118 Type: SSH_FXP_LSTAT, Size: 33, Number: 263
< 2012-09-28 16:36:07.118 Type: SSH_FXP_STATUS, Size: 34, Number: 263
< 2012-09-28 16:36:07.118 Status code: 3, Message: 263, Server: Permission denied, Language:
< 2012-09-28 16:36:07.118 Script: Can't get attributes of file 'C:\SFTPHome\hostedfiles'.
< 2012-09-28 16:36:07.118 Script: Permission denied.
< 2012-09-28 16:36:07.118 Error code: 3
< 2012-09-28 16:36:07.118 Error message from server: Permission denied
< 2012-09-28 16:36:07.118 Request code: 7
. 2012-09-28 16:36:07.118 Script: Failed
> 2012-09-28 16:36:09.463 Script: exit
. 2012-09-28 16:36:09.464 Script: Exit code: 1
. 2012-09-28 16:36:09.464 Closing connection.
. 2012-09-28 16:36:09.464 Sending special code: 12
. 2012-09-28 16:36:09.464 Sent EOF message
09-28-2012 16:36:09 HOST LT-CND0411CDW.ResoluteEnergy.com SSH sftpuser2 disconnected.