关于FTP、FTPS 与 SFTP 简介见另外一篇转载的文章,讲的比较详细,此文不在另外描述.
https://blog.csdn.net/ericwuhk/article/details/109120158
FTPS在微软官网寥寥数笔,看完也一头雾水,无奈找了一通,找了到了一份开源代码,研究了一通发觉可用,至此记录.
https://docs.microsoft.com/en-us/archive/blogs/adarshk/using-ftpwebrequest-to-do-ftp-over-ssl
一句未改得原始代码上传至CSDN,以便哪天找不到国外网站资源.
https://download.csdn.net/download/ericwuhk/12984869
第一步:打开源码,VS报弹框不用理,直接确定即可.在原始代码中直接rebuild编译FTPSClient,生成"AlexPilotti.FTPS.Client.dll"和“AlexPilotti.FTPS.Client.XML" 这两个文件导入至所需得项目路径下,直接引用就可.
第二步:编译FTPSClientCmdApp,生成ftps.exe,打开cmd命令窗口查看工具是否可正常使用
ftps -h 10.131.128.6 -U user -P password -ssl CredentialsRequired -g /Modules/ZW01TLAA.IMZ C:\Users\XXX\Desktop\mytest
第三步:在自己的项目中,导入"AlexPilotti.FTPS.Client.dll"和“AlexPilotti.FTPS.Client.XML"后,需要修改代码才能正确使用.核心代码修改如下:
using System;
using System.Data;
using System.IO;
using BaseLibrary.ExecutionResults;
using System.Text;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using BaseLibrary.Config;
using AlexPilotti.FTPS.Client;
public class readfile
{
public string strServerPath;
public FTPSClient client;
public void initFTPS()
{
CCfg ccfg = new CCfg();
strServerPath = ccfg.GetValue("serverPath");//modules
string user = ccfg.GetValue("user");//"ftpsuser"
string password = ccfg.GetValue("password");//"password"
string ip = ccfg.GetValue("ip");//"192.168.1.1"
client = new FTPSClient();
NetworkCredential credential = new NetworkCredential(user, password);
ESSLSupportMode sslRequestSupportMode = ESSLSupportMode.DataChannelRequested;
X509Certificate x509ClientCert = null;
string sslClientCertPath = null;
int sslMinKeyExchangeAlgStrength = 0;
int sslMinCipherAlgStrength = 0;
int sslMinHashAlgStrength = 0;
int timeout = 120;
bool useCtrlEndPointAddressForData = false;
EDataConnectionMode dataConnectionMode = EDataConnectionMode.Passive;
if (sslClientCertPath != null)
{
x509ClientCert = X509Certificate.CreateFromCertFile(sslClientCertPath);
}
client.Connect(ip, 21,
credential,
sslRequestSupportMode,
new RemoteCertificateValidationCallback(ValidateTestServerCertificate),
x509ClientCert,
sslMinKeyExchangeAlgStrength,
sslMinCipherAlgStrength,
sslMinHashAlgStrength,
timeout * 1000,
useCtrlEndPointAddressForData,
dataConnectionMode);
}
enum EInvalidSslCertificateHandling { Refuse, Accept, Prompt }
private bool ValidateTestServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
bool certOk = false;
EInvalidSslCertificateHandling sslInvalidServerCertHandling = EInvalidSslCertificateHandling.Prompt;
if (sslPolicyErrors == SslPolicyErrors.None)
certOk = true;
else
{
Console.Error.WriteLine();
if ((sslPolicyErrors & SslPolicyErrors.RemoteCertificateChainErrors) > 0)
Info("WARNING: SSL/TLS remote certificate chain warning");
if ((sslPolicyErrors & SslPolicyErrors.RemoteCertificateNameMismatch) > 0)
Info("WARNING: SSL/TLS remote certificate name mismatch");
if ((sslPolicyErrors & SslPolicyErrors.RemoteCertificateNotAvailable) > 0)
Info("WARNING: SSL/TLS remote certificate not available");
if (sslInvalidServerCertHandling == EInvalidSslCertificateHandling.Accept)
certOk = true;
}
if (!certOk)
{
Info("SSL/TLS Server certificate details:");
Info(GetCertificateInfo(certificate));
}
if (!certOk && sslInvalidServerCertHandling == EInvalidSslCertificateHandling.Prompt)
{
//certOk = Utility.ConsoleConfirm("Accept invalid server certificate? (Y/N)");
certOk = true;
}
return certOk;
}
public static string GetCertificateInfo(X509Certificate certificate)
{
StringBuilder certInfo = new StringBuilder();
//Note: certificate.ToString() returns just the class name in Mono 2.0
// Simulate the .Net frameworks 2.0 ToString()
certInfo.AppendLine("[Subject]");
certInfo.AppendLine(certificate.Subject);
certInfo.AppendLine("");
certInfo.AppendLine("[Issuer]");
certInfo.AppendLine(certificate.Issuer);
certInfo.AppendLine("");
certInfo.AppendLine("[Serial Number]");
certInfo.AppendLine(certificate.GetSerialNumberString());
certInfo.AppendLine("");
certInfo.AppendLine("[Not Before]");
certInfo.AppendLine(certificate.GetEffectiveDateString());
certInfo.AppendLine("");
certInfo.AppendLine("[Not After]");
certInfo.AppendLine(certificate.GetExpirationDateString());
certInfo.AppendLine("");
certInfo.AppendLine("[Thumbprint]");
certInfo.AppendLine(certificate.GetCertHashString());
return certInfo.ToString();
}
public void Download(string strServerFile,string strLocalFile)
{
if (!File.Exists(strLocalFile))
{
if (client == null)
{
initFTPS();
}
Info($"{strServerFile} is copying.");
client.GetFile($"/{strServerPath}/{strServerFile}", strLocalFile);
}
else
{
Info($"{strServerFile} is already existed!");
}
}
}
是不是相当简单呢?