【问题标题】:Upload file to FTPS in c#.net在 c#.net 中将文件上传到 FTPS
【发布时间】:2019-12-06 19:41:32
【问题描述】:

任何人都可以提供简短指南如何使用 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

【问题讨论】:

标签: c# .net iis ftps self-signed-certificate


【解决方案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();
}

到目前为止它可以工作,但是I am not sure if the validation code is secure enough

在 FluentFTP 的常见问题解答中,您可以找到有关 client certificates to login with FTPS 的使用信息。

【讨论】:

  • 当我将 FtpEncryptionMode 挂起为隐式时,它开始工作。你知道这是为什么吗?
  • 编辑:当我将 FTPS 服务器端口更改为 990 以外的端口时,我可以使用显式模式,但我对正在发生的事情的了解更少。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-04
  • 1970-01-01
  • 2020-09-07
  • 1970-01-01
  • 1970-01-01
  • 2020-05-14
  • 2019-06-26
相关资源
最近更新 更多