WinSCP authentication using private key C#

Environment:

I was trying to connect to WinSCP from my Web Application using C# however I kept on getting an unknown server error. After doing some research (of which I did not find much), my dear friend helped me out figuring out the solution. So here it goes..

Error:

winscp> open "name@host.com:22" -privatekey=C:\keys\private.ppk
Searching for host...
Connecting to host...
Authenticating...
Continue connecting to an unknown server and add its host key to a cache?
The server's host key was not found in the cache. You have no guarantee that the server is the computer you think it is.
The server's dss key fingerprint is:
ssh-dss 1024 68:57:83:42:c9:42:08:db:13:0d:80:ec:8b:e2:43:c0
If you trust this host, press Yes. To connect without adding host key to the cache, press No. To abandon the connection press Cancel.
(Y)es, (N)o, C(a)ncel, (C)opy Key: Cancel
Host key wasn't verified!
Host key fingerprint is ssh-dss 1024 68:57:83:42:c9:42:08:db:13:0d:80:ec:8b:e2:43:c0.

Authentication failed.

Solution:

  • Open WinSCP in windows.
  • Login in to the server you would like to connect from C#.
  • Click the Advanced Button

login

 

  • Go to the “Authentication” Tab
  • Browse for the private key, click “Ok”

private key

  • Save (optional)
  • Login
  • Once you click “Login”, it will ask you whether you would like to authorize connection/accept the key with server. Agree.
  • Now that your server is authenticated, you can go back to your code in C#.
  • Open your web.config
  • In the <system.web> section add the following:
  • <identity impersonate=”true” userName=”my-pc” password=”my-password”/>
  • Save.
  • Run code again and the above error should disappear.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s