2009-10-22 94 views
5

我實現了我的自定義FTP類以與我付費的託管服務器一起工作。 我使用FTP進行備份,恢復和更新我的應用程序。 我現在正在開始使用ssl來投入生產。 我問我的託管公司是否支持SSL協議,他們說他們這樣做。帶EnableSSL的FtpWebRequest

所以我修改微軟MSDN的教程後,我的方法是這樣的:

reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(m_ftpAddress.Trim())); 
reqFTP.UseBinary = true; 
reqFTP.Credentials = new NetworkCredential(m_ftpUsername, m_ftpPassword); 
reqFTP.Method = WebRequestMethods.Ftp.ListDirectory; 

ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(ValidateServerCertificate); 
       X509Certificate cert = new X509Certificate(path to a certificate created with makecert.exe); 

reqFTP.ClientCertificates.Add(cert); 
reqFTP.AuthenticationLevel = AuthenticationLevel.MutualAuthRequested; 
reqFTP.ImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Identification; 

reqFTP.EnableSsl = true; 

現在,MSDN說,如果服務器支持SSL協議時AUTH TLS問也不會拋出和異常。這可以在跟蹤日誌中看到。所以我想這不是服務器問題。

認證階段之後,服務器返回觸發一個錯誤

System.Net Information: 0 : [0216] FtpControlStream#41622463 - Received response [227 Entering Passive Mode (the IP and port number).]

消息:

System.Net Error: 0 : [0216] Exception in the FtpWebRequest#12547953::GetResponse - The remote server returned an error: 227 Entering Passive Mode (the IP and port number).

我嘗試設置

reqFTP.UsePassive = true; 

屬性設置爲false,然後我得到此錯誤:

System.Net Information: 0 : [2692] FtpControlStream#4878312 - Received response [500 Illegal PORT command]

當然,如果沒有將EnableSLL屬性設置爲true,那麼所有工作都沒有問題。

有沒有人有這方面的想法?

編輯:我修改了代碼爲法洛斯:

reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(m_ftpAddress.Trim())); 
reqFTP.UseBinary = true; 
reqFTP.Credentials = new NetworkCredential(m_ftpUsername, m_ftpPassword); 
reqFTP.Method = WebRequestMethods.Ftp.ListDirectory; 

ServicePointManager.ServerCertificateValidationCallback = new  
    System.Net.Security.RemoteCertificateValidationCallback(ValidateServerCertificate); 

reqFTP.ClientCertificates.Add(cert); 
reqFTP.AuthenticationLevel = AuthenticationLevel.MutualAuthRequested; 
reqFTP.ImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Identification 
reqFTP.EnableSsl = true; 

ValidateServerCertificate總是返回。在這個修改之後,效果是沒有的。

而且我不明白:

  • 在這一瞬間的應用程序正在使用服務器證書,對不對?
  • 並在修改之前也使用我的?

有人可以解釋我的工作原理嗎?

編輯:許多電子郵件與託管公司交換後,事實證明,他們有他們的FTP軟件的問題,這是沒有問題的代碼。

+1

然後發佈您的編輯作爲答案並接受它。 – 2012-04-06 23:48:02

回答

1

您是否檢查過您的主機是否使用FTPS或SFTP?

他們是不同的協議,你的主機可能會認爲你是在談論錯誤的協議。

+0

感謝您的回覆。我曾與託管公司打交道,問題解決了。事實證明,這是對他們的環境的一些設置。 – mosu 2010-11-12 08:20:36

+0

^您的託管環境是否會成爲EC2亞馬遜雲託管? – D3vtr0n 2011-03-28 21:02:35