由于FTP传输是用明文形式,故客户要求加密,所以研究了下连接带证书FTP的情况,在网上搜索了下资料,发现在一个英文网站上得到了启发(http://blogs.msdn.com/b/adarshk/archive/2005/04/22/410925.aspx),经过研究,已能正常连接带证书的FTP,把测试代码贴上,以让后来者有所帮助
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Security.Authentication;
using System.Net.Security;
namespace FTPForSSLTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 验证证书
/// </summary>
/// <param name="sender"></param>
/// <param name="certificate"></param>
/// <param name="chain"></param>
/// <param name="sslPolicyErrors"></param>
/// <returns></returns>
public static bool ValidateServerCertificate(object sender,X509Certificate certificate, X509Chain chain,SslPolicyErrors sslPolicyErrors)
{
//return true;
if (sslPolicyErrors ==
SslPolicyErrors.RemoteCertificateChainErrors)
{
return false;
}
else if (sslPolicyErrors ==
SslPolicyErrors.RemoteCertificateNameMismatch)
{
System.Security.Policy.Zone z =
System.Security.Policy.Zone.CreateFromUrl
(((FtpWebRequest)sender).RequestUri.ToString());
if (z.SecurityZone ==System.Security.SecurityZone.Internet)
{
return true;
}
return false;
}
return true;
}