I haven’t been writing to my blog for a long time. Last week I’ve used  .NET wrapper for a project. Usage of .NET wrapper written for WinSCP is very easy and there is a good documentation on that. So I won’t go through integrating winscp.dll to your c#.net project.

But the exact problem is hostkey. When you connect a remote server for first time, the client application asks you if you want to connect this server — yes or no ??? (it asks this question with server’s host name). And if you say yes to this question, you can connect to this server, other way it is not possible and you get authentication error. But please differ that; it is not an authentication regarding the server, it is an authentication to defense the client side.

The problem is it is not possible saying yes to the question of “do you know this host key?” with using dll of winscp. Because of that if you did not give exactly right host key with the SessionOptions object, you are in trouble.

In the function below, I got the server hostkey in catch code block, and I checked if this is an hostkey error or not, and then if it is; I update my hostkey with the new one and try to connect the system with new parameters again.

“Session_ac()” is my function, that I call to initialize my connection, connection parameters and open the session.

private void SessionAc()
        {
            try
            {
                // Setup session options
                sessionOptions = new SessionOptions
                {
                    Protocol = Protocol.Sftp,
                    HostName = "ip_address_of_host",
                    UserName = "username",
                    Password = "password",
                    SshHostKey = "ssh-rsa " + label3.Text
                };

                //Initialize the Session object
                session = new Session();
                session.Open(sessionOptions);
                //If connection is OK, use a label as status and output the connected state.
                label2.Text = "Bağlantı Sağlandı";
            }
            catch (Exception k)
            {
                //get the exception as "k" and pass it to variable "txt". 
                string txt = k.InnerException.ToString();

                //if the error contains the words "ssh-rsa " and "Host key", we can be sure that it is
                //an error releated to Host Key issue that we mentioned.
                if (txt.Contains("ssh-rsa ") && txt.Contains("Host key"))
                {
                    //label3 is the output and carrier of its value for my hostkey. And I update the new
                    //hostkey that written in my exception k.
                    label3.Text = txt.Substring(txt.IndexOf("1024 "), label3.Text.Length);
                    //After updating my host key variable, now I can connect with new parameters.
                    SessionAc();
                }
                else
                    label2.Text = "Bağlantı Kapalı";
                    //If this error is not releated to hostkey, do nothing and update my status
                    //text as "connection is closed"
            }
        }