Failed to store new host key

Advertisement

divya.yandra
Joined:
Posts:
1

Failed to store new host key

Hi,

We are using WinSCP nuget (version: 5.19.3) in our project.
WinSCP nuget is used to connect to a SFTP server from Microsoft Azure function.
Our Microsoft Azure function is a timer trigger function. We are using C# .NET in the timer trigger function.
Our Azure function is working fine in local, but when we have hosted to Azure cloud it is giving below exception when trying to connect to the SFTP server.
Exception:
Messsage: Failed to store new host key. Authentication failed.
Exception type: WinSCP.SessionRemoteException
Failed Method: WinSCP.SessionLogReader.Read
Our Code:
SessionOptions sessionOptions = new SessionOptions
{
    Protocol = Protocol.Sftp,
    HostName = "xxx",
    UserName = "xx",
    Password = "xxx",
    PortNumber = "xx",
    SshHostKeyPolicy = SshHostKeyPolicy.AcceptNew
};
 
using (session = new Session())
{                    
    session.Open(sessionOptions);
    
    TransferOptions transferOptions = new TransferOptions();
    transferOptions.TransferMode = TransferMode.Binary;
    transferOptions.ResumeSupport.State = TransferResumeSupportState.Off;
 
    var localTempPath = $"{Directory.GetCurrentDirectory()}\\{ sftpServer.SFTP.FileName}";
 
    // Download the file
    TransferOperationResult transferOperationResult = session.GetFiles($"{ sftpServer.SFTP.Directory}{ sftpServer.SFTP.FileName}", localTempPath, false, transferOptions);
    
 
    // Read the file
    result = File.ReadAllLines(localTempPath).Select(v => ImportSampleModel.FromCsv(v)).ToList();
 
    // Delete the file
    File.Delete(localTempPath);
}

Reply with quote E-mail

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
41,518
Location:
Prague, Czechia

Re: Failed to store new host key

The SshHostKeyPolicy.AcceptNew uses Windows registry to store the host key. It's quite likely that the registry is not writable in the limited Azure function environment. It makes sense as the environment is not persistent. Even if it was writtable, the host key won't be persisted between runs. So it would be as good/bad as using SshHostKeyPolicy.GiveUpSecurityAndAcceptAny.

See also https://winscp.net/eng/docs/faq_hostkey#automation

Reply with quote

Advertisement

You can post new topics in this forum