title: Converting WinSCP script to C# code using WinSCP .NET assembly
body: <p>I've been downloading files from an external server using WinSCP for over a year by firing out external scripts from my C# code on a daily task run automatically.</p>
  
  <p>I recently upgraded to version 5.5.6 (from V4.3.5.1463) so that I could make use of the .NET library for this and for a new SFTP task have for a different server. The SFTP task on the new server works really well using the .NET library. I'm very pleased with it, but the old script code now fails.</p>
  
  <p>I've tried adding .NET code just to log in and get a list of files and that fails too. However, if I run the winscp UI directly, I can see the files on the server and download them. I used the same details in my code as I did in the UI, including the key. The code is obviously connecting to the server and the key is confirmed, but then fails to authenticate! Can anyone help?</p>
  
  <p>Notes: I have copied the <code>winscp.exe</code> into the same folder as the .NET library. My account doesn't need a password.</p>
  
  <p>I have also created a log file, follows...</p>
  
  <p>Note, for security, in this log, I've changed my username, the connection IP address and the key.</p>
  
  <pre class="lang-none prettyprint-override"><code>. 2015-01-19 14:24:00.757 --------------------------------------------------------------------------
  . 2015-01-19 14:24:00.757 WinSCP Version 5.5.6 (Build 4746) (OS 6.1.7601 Service Pack 1 - Windows Server 2008 R2 Standard)
  . 2015-01-19 14:24:00.757 Configuration: nul
  . 2015-01-19 14:24:00.757 Local account: SERVER\admin
  . 2015-01-19 14:24:00.757 Working directory: C:\Users\admin\Desktop\Debug\Debug
  . 2015-01-19 14:24:00.757 Process ID: 4444
  . 2015-01-19 14:24:00.757 Command-line: "C:\Users\admin\Desktop\Debug\Debug\winscp.exe" /xmllog="C:\Users\admin\AppData\Local\Temp
  
  \2\wscp0B6C.01408C35.tmp" /xmlgroups /nointeractiveinput /dotnet=556  /ini=nul /log="C:\mylet\website\Temp\WinScpLog.txt"  /console 
  
  /consoleinstance=_2924_54848996_804
  . 2015-01-19 14:24:00.757 Time zone: Current: GMT+0, Standard: GMT+0 (GMT Standard Time), DST: GMT+1 (GMT Daylight Time), DST Start: 
  
  29/03/2015, DST End: 25/10/2015
  . 2015-01-19 14:24:00.757 Login time: 19 January 2015 14:24:00
  . 2015-01-19 14:24:00.757 --------------------------------------------------------------------------
  . 2015-01-19 14:24:00.757 Script: Retrospectively logging previous script records:
  &gt; 2015-01-19 14:24:00.757 Script: option batch on
  &lt; 2015-01-19 14:24:00.757 Script: batch           on        
  &gt; 2015-01-19 14:24:00.757 Script: option confirm off
  &lt; 2015-01-19 14:24:00.757 Script: confirm         off       
  &gt; 2015-01-19 14:24:00.757 Script: open -hostkey="ssh-dss 1024 aa:aa:aa:aa:aa:aa:aa:aa:aa:aa:aa:aa:aa:aa:aa:aa" -timeout=15 
  
  "sftp://User@1.1.1.1:22"
  . 2015-01-19 14:24:00.757 --------------------------------------------------------------------------
  . 2015-01-19 14:24:00.757 Session name: user@1.1.1.1 (Ad-Hoc site)
  . 2015-01-19 14:24:00.757 Host name: 1.1.1.1 (Port: 22)
  . 2015-01-19 14:24:00.757 User name: user (Password: No, Key file: No)
  . 2015-01-19 14:24:00.757 Tunnel: No
  . 2015-01-19 14:24:00.757 Transfer Protocol: SFTP
  . 2015-01-19 14:24:00.757 Ping type: -, Ping interval: 30 sec; Timeout: 15 sec
  . 2015-01-19 14:24:00.757 Proxy: none
  . 2015-01-19 14:24:00.757 Send buffer: 262144
  . 2015-01-19 14:24:00.757 SSH protocol version: 2; Compression: No
  . 2015-01-19 14:24:00.757 Bypass authentication: No
  . 2015-01-19 14:24:00.757 Try agent: Yes; Agent forwarding: No; TIS/CryptoCard: No; KI: Yes; GSSAPI: No
  . 2015-01-19 14:24:00.757 Ciphers: aes,blowfish,3des,WARN,arcfour,des; Ssh2DES: No
  . 2015-01-19 14:24:00.757 SSH Bugs: A,A,A,A,A,A,A,A,A,A
  . 2015-01-19 14:24:00.757 Simple channel: Yes
  . 2015-01-19 14:24:00.757 Return code variable: Autodetect; Lookup user groups: A
  . 2015-01-19 14:24:00.757 Shell: default
  . 2015-01-19 14:24:00.757 EOL: 0, UTF: 2
  . 2015-01-19 14:24:00.757 Clear aliases: Yes, Unset nat.vars: Yes, Resolve symlinks: Yes
  . 2015-01-19 14:24:00.757 LS: ls -la, Ign LS warn: Yes, Scp1 Comp: No
  . 2015-01-19 14:24:00.757 Local directory: default, Remote directory: home, Update: Yes, Cache: Yes
  . 2015-01-19 14:24:00.757 Cache directory changes: Yes, Permanent: Yes
  . 2015-01-19 14:24:00.757 DST mode: 1; Timezone offset: 0h 0m
  . 2015-01-19 14:24:00.757 --------------------------------------------------------------------------
  . 2015-01-19 14:24:00.757 Looking up host "1.1.1.1"
  . 2015-01-19 14:24:00.757 Connecting to 1.1.1.1 port 22
  . 2015-01-19 14:24:00.804 Server version: SSH-2.0-0.0
  . 2015-01-19 14:24:00.804 Using SSH protocol version 2
  . 2015-01-19 14:24:00.804 We claim version: SSH-2.0-WinSCP_release_5.5.6
  . 2015-01-19 14:24:00.819 Doing Diffie-Hellman group exchange
  . 2015-01-19 14:24:01.053 Doing Diffie-Hellman key exchange with hash SHA-1
  . 2015-01-19 14:24:01.365 Verifying host key dss 0x91486c77af8989ad 094f4a206efd983a ceeaae78321795b5 db1bf3a553007097 e1c8a86c09b41f62 
  
  d93ca9af3dc8b5bd c10b6effc00da737 460fd7cea6c9491c 805ed7e13fbd423f 6ec239a74e66c8ff 16a199166f7076d8 f4e2874eeaf66f6c 6ba4d53436e4fed4 
  
  68f44d7062052513 51f4eefc6e64c9f9 42e1fd1dc4c693f1 ,0x952e1dd3f13c9c9c e5d3fe2c7c96c137 173db4c9,0x88fc0112369215b4 0f4c670c3801f5e7 
  
  08cbe3a8110c33f4 d6e6bf4880e8baa0 b1f0064f3808ebbc 286727e38bd4737f 32a67cb19b2851cc 58a87fd7166fdd0d 2524be55588b40a2 f78bba20c6db1049 
  
  5b36125b0a1a9c94 31ce3c64a23cd028 94624932d350bb4c 056649e7ba10a807 f82037d5a4993340 3f511e923d1e031,0x2499ade72a6348f8 0919ccb5e56c68fd 
  
  297191be353b28b7 4a7b5d55d461486d 8b86d0696617a74a a4055e1cf15baa2e 54afbb40223a6f7e 4500d7649bf51410 39a84eb81023d550 4bbd7cea6d4eb8f7 
  
  8244bd3e8fa48387 90390b3cf3dd60a7 c45a6ddd967a8165 cf01da8309042e84 62eb7d511dd00348 ce127fdbc371d3c5  with fingerprint ssh-dss 1024 
  
  92:ee:83:c2:30:33:de:f4:51:f2:c9:3f:ae:cd:91:9c
  . 2015-01-19 14:24:01.365 Host key matches configured key
  . 2015-01-19 14:24:01.365 Host key fingerprint is:
  . 2015-01-19 14:24:01.365 ssh-dss 1024 aa:aa:aa:aa:aa:aa:aa:aa:aa:aa:aa:aa:aa:aa:aa:aa
  . 2015-01-19 14:24:01.365 Initialised AES-256 SDCTR client-&gt;server encryption
  . 2015-01-19 14:24:01.365 Initialised HMAC-SHA1 client-&gt;server MAC algorithm
  . 2015-01-19 14:24:01.365 Initialised AES-256 SDCTR server-&gt;client encryption
  . 2015-01-19 14:24:01.365 Initialised HMAC-SHA1 server-&gt;client MAC algorithm
  ! 2015-01-19 14:24:01.553 Using username "user".
  . 2015-01-19 14:24:01.584 Prompt (7, SSH password, , &amp;Password: )
  . 2015-01-19 14:24:01.584 Disconnected: Unable to authenticate
  </code></pre>
  
  <p>Here's my C# code:</p>
  
  <pre class="lang-csharp prettyprint-override"><code>SessionOptions sessionOptions = new SessionOptions
  {
     Protocol = Protocol.Sftp,
     HostName = "1.1.1.1",
     PortNumber = 22,
     UserName = "user",
     SshHostKeyFingerprint = "ssh-dss 1024 aa:aa:aa:aa:aa:aa:aa:aa:aa:aa:aa:aa:aa:aa:aa:aa"
  };
  
  using (Session session = new Session())
  {
     session.Open(sessionOptions); // the failure happens on this line
  }
  </code></pre>