ฉันกำลังพยายามใช้ชุดประกอบ WinSCP เพื่ออัปโหลดไปยังเซิร์ฟเวอร์ FTP ของเรา เรามีคีย์ส่วนตัวทั่วไปจัดเก็บไว้ในฐานข้อมูลของเราที่เราดึงข้อมูลและใช้สำหรับการตรวจสอบสิทธิ์ (ขณะนี้ใช้งานได้กับการอัปโหลด Java usith JSch
) อย่างไรก็ตาม WinSCP ต้องการชื่อพาธของไฟล์
ฉันกำลังพยายามเขียนคีย์ส่วนตัวของเราลงในไฟล์เพื่อให้ WinSCP ใช้สำหรับการตรวจสอบสิทธิ์ แต่ฉันได้รับข้อผิดพลาดต่อไปนี้:
"C:\[..path..].\key.txt (ไม่ใช่รูปแบบไฟล์คีย์ที่รู้จัก)"
รหัสอัปโหลด:
String host = token.hostName;
String user = token.userID;
String sshKey = token.sshKey;
//write key to temp file
String keyFile = AppDomain.CurrentDomain.BaseDirectory+@".\key.txt";
FileStream temp = new FileStream(keyFile, FileMode.Create);
temp.Write(Encoding.ASCII.GetBytes(sshKey), 0 , 0);
temp.Close();
try {
// Setup session options
SessionOptions sessionOptions = new SessionOptions {
Protocol = Protocol.Sftp,
HostName = host,
UserName = user,
SshPrivateKeyPath = keyFile,
GiveUpSecurityAndAcceptAnySshHostKey = true
};
using (Session session = new Session()) {
// Connect
session.Open(sessionOptions);
session.DisableVersionCheck = true;
// Upload files
TransferOptions transferOptions = new TransferOptions();
transferOptions.TransferMode = TransferMode.Binary;
TransferOperationResult transferResult;
transferResult = session.PutFiles(@resultsFolder+"*", depthDir, false, transferOptions);
// Throw on any error
transferResult.Check();
// Print results
foreach (TransferEventArgs transfer in transferResult.Transfers) {
Console.WriteLine("Upload of {0} succeeded", transfer.FileName);
}
}
} catch (Exception e) {
Console.WriteLine("Error: {0}", e);
} finally {
// delete file with key
File.Delete(keyFile);
}
key.txt
ของคุณมีรูปแบบใด แสดงให้เราเห็น - person Martin Prikryl   schedule 15.06.2018key.txt
เป็นเพียงสตริงอักขระยาว (คีย์ส่วนตัว) - person Fueled By Coffee   schedule 15.06.2018