2013-10-11 5 views
1

FileZilla를 서버 및 DNS 서비스로 사용하고 있으므로 로컬 컴퓨터 IP를 사용할 필요가 없습니다. (그러나 다음 두 가지 방법 모두 시도했습니다.)FTPS (SSL)의 인증서 유효성 검사/설치?

System.Net.FtpWebRequest가 작동하도록 시도한 후에 나는 (SO에 대한 몇 가지 게시물을 포함하여) 주위를 읽었으며 SSL 지원이 해당 라이브러리에 적합하지 않음을 발견했습니다. 그것은 일반 FTP 작업을했지만, 나는 SSL을 강요하려고 할 때, 내가 말하는 인증서 유효성 검사 오류가 발생했습니다 : The remote certificate is invalid according to the validation procedure.

그래서, 나는 몇 가지 주변 검색을 수행하고 알렉스 FTPS 클라이언트 라이브러리를 발견했습니다. 다음은 내가 작성한 코드입니다.

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 서버에 완벽하게 액세스 할 수 있습니다. 그래서, 나는 증명서 문제가 있어야 할 것이라고 생각했습니다.

내 서버는 다음 로그 항목 보여주는 것을주의해야한다 : 이것은 절대적으로 동일한 오류가

The remote certificate is invalid according to the validation procedure. 

: 클라이언트 (C# WPF 응용 프로그램)이 오류가 발생 동안

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

을 .NET 라이브러리와 MSDN 코드를 사용하면됩니다.

는 좀 더 연구를 다음과 비슷한 솔루션을 발견했습니다

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

The remote certificate is invalid according to the validation procedure 그러나 그들은 단지 위험 해킹처럼 보인다 ... 그리고 그들은이 일을하면서, 거기입니다 인증 정보를 표시하고 사용자가 유효성을 검사하도록 설정하는 방법/현재 사용중인 기본 예/아니오 외에 설치 하시겠습니까?

지금 (I 알렉스의 라이브러리를 도랑 및 기본 .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; 
     } 
    } 
} 
+1

귀하의 코드는 검사를 위해 사용자에게 인증서를 절대적으로 제공 할 수 있으며 유효성 검사 콜백에서 코드에 반환되는 인증서 유효성 확인과 관련된 특정 문제를 표시해야합니다. – EricLaw

답변

4

그래서, 같은 문제를 가지고 사람을 위해, 난 그냥 사용자에게 대한 경고를주고 결국 인증서 및 원래 게시물에서 제공 한 링크를 기반으로 수락 또는 거부 할 수있는 옵션이 있습니다. 인증서의 유효성을 검사하려면 인증서가 로컬이 아닌 실제 인증서 여야합니다. 이제는 그게 유일한 해결 방법입니다.

-1

Alex ftps는 지정한 경우 동일한 인증서 유효성 검사를 수행합니다. 당신의 client.connect에서 인증서 그런 다음 아래

client.Connect(ftpIP, new NetworkCredential(ftpUser, ftpPass), 
           ESSLSupportMode.CredentialsRequired | ESSLSupportMode.DataChannelRequested, 
          new RemoteCertificateValidationCallback(ValidateTestServerCertificate)); 

을 허용하도록 remotecertificatevalidationcallback를 추가합니다.

private static bool ValidateTestServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) 
    { 
     // Accept any certificate 
     return true; 
    } 

기본 .net을 사용하고 싶습니다. 하지만 암시 적으로 사용하는 서버에 연결되어 있습니다. :

+0

그래, 나는 그의 라이브러리가 그것을하지 않을 것이라고 말하지 않았다. .NET에 의해 제공되는 라이브러리가 그 특별한 상황에서 같은 방식으로 작동했기 때문에, 나는 그 라이브러리가 필요 없다는 것을 의미한다. 원본 게시물에서 제공 한 링크는 귀하가 제공 한 동일한 솔루션으로 연결됩니다. 나는 그 (것)들이 조용히 증명서를 받아 들였기 때문에, 다만 그들을 좋아하지 않았다. 분명히 사용자에게 계속 알릴 수는 있지만 콜백을 시작하는 것은 추가 작업 일뿐입니다. 어느 쪽이든, 실제 서명 된 인증서가 아닌 로컬로 생성 된 인증서를 수락하고 있습니다. 그의 라이브러리를 사용하는 것이 더 짧은 방법입니다. –