1

谁能提供简短指南如何使用 C# 中的自签名证书将文件发送到 FTPS 服务器?我很快就通过 SFTP 和 FTP 处理了文件传输,但我会对 FTPS 失去理智。我已经用 FtpWebRequest 和 fluentFtp 尝试了几天,但我不确定我的代码是否错误或者我在机器上配置 FTPS 的方式,所以我真的很想通过这两个指南。下面我提供了我测试过的异常代码。

    public bool Send(string filePath)//webRequest version
    {
        bool isFileUploaded = false;

        try
        {
            FtpWebRequest request = WebRequest.Create($"ftp://{this.DestinationInfo.HostIp}:{this.DestinationInfo.Port}/ftps_test/test.txt") as FtpWebRequest;
            request.Credentials = this.Credentials;              //hostIp is the same machine Ip     // port is 990
            request.EnableSsl = true;
            request.Method = WebRequestMethods.Ftp.UploadFile;

            using (Stream fileStream = File.OpenRead(filePath))
            using (Stream ftpStream = request.GetRequestStream())
            {
                fileStream.CopyTo(ftpStream);
            }
            isFileUploaded = true;
        }
        catch (Exception exception)
        {
            //logging -> exception.Message is "System error" and nothing else
            isFileUploaded = false;
        }

        return isFileUploaded;
    }

    public bool Send(string filePath)// fluentFtp version
    {
        bool isFileUploaded = false;

        try
        {
            FtpClient client = new FtpClient(this.DestinationInfo.HostIp, this.DestinationInfo.UserName, this.DestinationInfo.Password);
                                            //also tried "ftp://{HostIp}"
            client.Port = this.DestinationInfo.Port;//990
            client.ReadTimeout *= 10; //temporary because of timeout
            client.EncryptionMode = FtpEncryptionMode.Implicit;
            client.SslProtocols = SslProtocols.Default;
            client.Connect();
            client.UploadFile(filePath, "test.csv");

            isFileUploaded = true;
        }
        catch (Exception exception)
        {
            //logging -> exception.Message is "According to the validation procedure, the remote certificate is invalid.", but i have not idea why
            isFileUploaded = false;
        }

        return isFileUploaded;
    }

出于我的目的,我只能使用免费的 nugets

4

1 回答 1

1

我将 FluentFTP 和以下代码与证书颁发机构签署的证书结合使用:

private FtpClient getFtpsClient(System.Uri uri) {
    if (uri.Scheme != "ftps") {
        throw new NotImplementedException("Only ftps is implementent");
    }
    var userInfo = uri.UserInfo.Split(":");
    FtpClient client = new FtpClient(uri.Host, userInfo[0], userInfo[1]);
    client.EncryptionMode = FtpEncryptionMode.Explicit;
    client.SslProtocols = SslProtocols.Tls;
    client.ValidateCertificate += new FtpSslValidation(OnValidateCertificate);
    client.Connect();

    void OnValidateCertificate(FtpClient control, FtpSslValidationEventArgs e) {
        var cert2 = new X509Certificate2(e.Certificate);
        e.Accept = cert2.Verify();
    }
    return client;
}

private void writeFileFtps(System.Uri uri, string fileName, byte[] content)
{
    var ftpsClient = getFtpsClient(uri);
    ftpsClient.Upload(content, uri.LocalPath + "/" + fileName);
    ftpsClient.Disconnect();
}

到目前为止它有效,但我不确定验证码是否足够安全

在 FluentFTP 的常见问题解答中,您可以找到有关使用客户端证书使用 FTPS 登录的信息。

于 2019-07-29T13:31:12.440 回答