Connect to FTPS with proxy in C #

My code below works fine on my computer without a proxy. But on the client server, they need to add a proxy for the FTP client (FileZilla) in order to be able to access FTP. But when I add a proxy, it says

SSL cannot be enabled when using a proxy.

FTP proxy

var proxyAddress = ConfigurationManager.AppSettings["ProxyAddress"];
WebProxy ftpProxy = null;
if (!string.IsNullOrEmpty(proxyAddress))
{
   var proxyUserId = ConfigurationManager.AppSettings["ProxyUserId"];
   var proxyPassword = ConfigurationManager.AppSettings["ProxyPassword"];
    ftpProxy = new WebProxy
    {
        Address = new Uri(proxyAddress, UriKind.RelativeOrAbsolute),
        Credentials = new NetworkCredential(proxyUserId, proxyPassword)
    };
 }

FTP connection

var ftpRequest = (FtpWebRequest)WebRequest.Create(ftpAddress);
ftpRequest.Credentials = new NetworkCredential(
                            username.Normalize(), 
                            password.Normalize()
                         );

ServicePointManager.ServerCertificateValidationCallback += 
   (sender, cert, chain, sslPolicyErrors) => true;

ServicePointManager.Expect100Continue = false;

ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
ftpRequest.EnableSsl = true;
//ftpRequest.Proxy = ftpProxy;
var response = (FtpWebResponse)ftpRequest.GetResponse();
+4
source share
1 answer

The .NET framework does not really support TLS / SSL connections through proxies.

You must use a third-party FTP library.

Also note that your code does not use “implicit” FTPS. It uses explicit FTPS. Implicit FTPS is also not supported by the .NET platform .


, WinSCP.NET, :

// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
    Protocol = Protocol.Ftp,
    HostName = "example.com",
    UserName = "user",
    Password = "mypassword",
    FtpSecure = FtpSecure.Explicit, // Or .Implicit
};

// Configure proxy
sessionOptions.AddRawSettings("ProxyMethod", "3");
sessionOptions.AddRawSettings("ProxyHost", "proxy");

using (Session session = new Session())
{
    // Connect
    session.Open(sessionOptions);

    var listing = session.ListDirectory(path);
}

SessionOptions.AddRawSettings . .

( WinSCP)

+2

Source: https://habr.com/ru/post/1691447/


All Articles