简体   繁体   English

如何使用密码或SSH指纹WinSCP C#.NET程序集进行SFTP身份验证

[英]How to SFTP authenticate using password or SSH fingerprint WinSCP C# .NET assembly

I'm trying to connect to a server with a SFTP connection, but I'm trying to authenticate with SSH fingerprint, if this is not correct, then should attempt with the SFTP password. 我正在尝试通过SFTP连接连接到服务器,但是我尝试使用SSH指纹进行身份验证,如果这不正确,则应尝试使用SFTP密码。

The issue that I'm having is that need both of them to access to the server, that should be different, if is not the SSH fingerprint, then try with the password, but is not working. 我遇到的问题是需要它们两者都可以访问服务器,这应该是不同的,如果不是SSH指纹,请尝试使用密码,但不能正常工作。

There is a way to validate first the fingerprint and if is not correct, validate the user password? 有一种方法可以首先验证指纹,如果不正确,请验证用户密码?

This is what I have: 这就是我所拥有的:

public string FilesSFTP_FTP()
{          
   TransferOptions TransferOption = new TransferOptions();
   TransferOperationResult TransferResult;
   SessionOptions sessionoptions = new SessionOptions();
   Session session = new Session();

   if (DataFile.sTransportType == "S")
   {
      sessionoptions.Protocol = Protocol.Sftp;
      sessionoptions.PortNumber = 22;
      sessionoptions.SshHostKeyFingerprint = DataFile.sFingerPrint;
   }
   else if (DataFile.sTransportType == "F")
   {
      sessionoptions.Protocol = Protocol.Ftp;
      sessionoptions.PortNumber = 21;
   }

   sessionoptions.HostName = DataFile.sIPAddress;
   sessionoptions.UserName = DataFile.sUserID;
   sessionoptions.Password = DataFile.sPassword;
   TransferOption.TransferMode = TransferMode.Binary;
   TransferOption.PreserveTimestamp = false;
   TransferOption.ResumeSupport.State = TransferResumeSupportState.Off;

   session.Open(sessionoptions);
}

There is another property that it need to be set? 还有另一个属性需要设置吗?

You cannot "authenticate with SSH fingerprint" . 您不能“使用SSH指纹进行身份验证”


The SessionOptions.SshHostKeyFingerprint is to verify the server's host key . SessionOptions.SshHostKeyFingerprint用于验证服务器的主机密钥 Not to authenticate the user. 不验证用户身份。

To authenticate the user, you need to use the SessionOptions.SshPrivateKeyPath . 要验证用户身份,您需要使用SessionOptions.SshPrivateKeyPath

See Understanding SSH key pairs to learn the difference. 请参阅了解SSH密钥对以了解不同之处。


As for your question. 至于你的问题。 You can set both the SessionOptions.SshPrivateKeyPath and the SessionOptions.Password . 您可以同时设置SessionOptions.SshPrivateKeyPathSessionOptions.Password WinSCP will first try the private key, and only if that fails, it will fall back to the password. WinSCP将首先尝试使用私钥,只有在私钥失败的情况下,它才会退回到密码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM