gpt4 book ai didi

c# - FTPS (SSL) 的证书验证/安装?

转载 作者:太空宇宙 更新时间:2023-11-03 13:12:48 24 4
gpt4 key购买 nike

我使用 FileZilla 作为服务器和 DNS 服务,这样我就不必使用我的本地机器 IP(但我已经在两者上尝试了以下方法)。

在尝试 System.Net.FtpWebRequest 工作后,我四处阅读(包括一些关于 SO 的帖子)并发现该库对 SSL 的支持不是很充分。它使用常规 FTP,但当我尝试强制使用 SSL 时,我收到证书验证错误消息:The remote certificate is invalid according to the validation procedure.

因此,我进行了一些搜索并找到了 Alex FTPS Client 库。这是我写的代码:

class FTPSWorker
{
public static void UploadFile(string sourceFile, string targetFile, string ftpIP, string ftpUser, string ftpPass)
{
try
{
using (FTPSClient client = new FTPSClient())
{
client.Connect(ftpIP, new NetworkCredential(ftpUser, ftpPass),
ESSLSupportMode.CredentialsRequired | ESSLSupportMode.DataChannelRequested);
client.SetTransferMode(ETransferMode.Binary);
client.PutFile(sourceFile, targetFile);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

不幸的是,我遇到了完全相同的证书错误。但是,我可以使用 FileZilla 客户端完美访问 FTP 服务器。因此,我认为必须存在证书问题。

我应该注意到我的服务器显示了以下日志条目:

Welcome Message
AUTH TLS
234 Using authentication type TLS
SSL connection established
disconnected

当客户端(C# WPF 应用程序)收到此错误时:

The remote certificate is invalid according to the validation procedure.

如果我使用 .NET 库和 MSDN 代码,这绝对是完全相同的错误。

我做了更多研究并找到了与这些类似的解决方案:

The remote certificate is invalid according to the validation procedure

"The remote certificate is invalid according to the validation procedure." using Gmail SMTP server

但它们看起来像是有风险的 hacks...虽然它们确实有效,但除了当前使用的基本是/否之外,是否有办法显示认证信息并让用户验证/安装它?

我现在的代码(我放弃了 Alex 的库并返回到默认的 .NET):

ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(FTPWorker.ValidateServerCertificate);

public class FTPWorker
{
public static void UploadFile(string sourceFile, string targetFile, string ftpIP, string ftpUser, string ftpPass)
{
try
{
string filename = "ftp://" + ftpIP + "/test/" + targetFile;
FtpWebRequest ftpReq = (FtpWebRequest)WebRequest.Create(filename);
ftpReq.Method = WebRequestMethods.Ftp.UploadFile;
ftpReq.Credentials = new NetworkCredential(ftpUser, ftpPass);
ftpReq.UsePassive = true;
ftpReq.EnableSsl = true;
ftpReq.UseBinary = true;
ftpReq.KeepAlive = false;

byte[] b = File.ReadAllBytes(sourceFile);

ftpReq.ContentLength = b.Length;

using (Stream s = ftpReq.GetRequestStream())
{
s.Write(b, 0, b.Length);
}

FtpWebResponse ftpResp = (FtpWebResponse)ftpReq.GetResponse();

if (ftpResp != null)
{
MessageBox.Show(ftpResp.StatusDescription);
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}

public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
if (sslPolicyErrors == SslPolicyErrors.None)
return true;
else
{
if (System.Windows.Forms.MessageBox.Show("The server certificate is not valid.\nAccept?",
"Certificate Validation", System.Windows.Forms.MessageBoxButtons.YesNo,
System.Windows.Forms.MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
return true;
else
return false;
}
}
}

最佳答案

因此,对于遇到相同问题的任何人,我最终只是向用户发出有关证书的警告以及根据我在原始帖子中提供的链接选择接受或拒绝的选项。为了验证证书,它必须是真实的,而不是本地创建的。所以,这是目前唯一的解决方法。

关于c# - FTPS (SSL) 的证书验证/安装?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19327840/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com