2017-11-25 11 views
3

우리는 오류요청이 SSL/TLS를 공유 호스팅 서버의 C#에 보안 채널을 만들 수 없습니다 중단되었습니다

The underlying connection was closed: An unexpected error occurred on a receive.System.Net.WebException: 또는 could not create SSL/TLS secure channel on server

우리의 코드를 아래에 보여 webrequest 또는 htmlagilitypack를 사용하여 HTTPS 서버에 연결할 수 없습니다 localhost에서 잘 작동하며 코드 파일에서 다음 부분을 추가했지만 서버에서만 발생하는 이유를 확인할 수는 없습니다.

ServicePointManager.Expect100Continue = true; 

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; 

누구든지 이에 대한 아이디어가 있으면 Google에 공유하십시오.

답변

0

가끔은 webrequest가 자체 서명 된 인증서를 수락하지 않기 때문입니다. 나는이 싱글 톤 클래스를 보통 사용한다. 자체 서명 된 모든 인증서를 허용합니다. 액세스하려는 URL을 공유하는 경우 더 간단한 해결책이 있는지 쉽게 판단 할 수 있습니다.

public sealed class Certificates 
{ 
    private static Certificates instance = null; 
    private static readonly object padlock = new object(); 

    Certificates() 
    { 
    } 

    public static Certificates Instance 
    { 
     get 
     { 
      lock (padlock) 
      { 
       if (instance == null) 
       { 
        instance = new Certificates(); 
       } 
       return instance; 
      } 
     } 
    } 
    public void GetCertificatesAutomatically() 
    { 
     ServicePointManager.ServerCertificateValidationCallback += 
      new RemoteCertificateValidationCallback((sender, certificate, chain, policyErrors) 
       => { return true; }); 
    } 

    private static bool RemoteCertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) 
    { 
     //Return true if the server certificate is ok 
     if (sslPolicyErrors == SslPolicyErrors.None) 
      return true; 

     bool acceptCertificate = true; 
     string msg = "The server could not be validated for the following reason(s):\r\n"; 

     //The server did not present a certificate 
     if ((sslPolicyErrors & 
      SslPolicyErrors.RemoteCertificateNotAvailable) == SslPolicyErrors.RemoteCertificateNotAvailable) 
     { 
      msg = msg + "\r\n -The server did not present a certificate.\r\n"; 
      acceptCertificate = false; 
     } 
     else 
     { 
      //The certificate does not match the server name 
      if ((sslPolicyErrors & 
       SslPolicyErrors.RemoteCertificateNameMismatch) == SslPolicyErrors.RemoteCertificateNameMismatch) 
      { 
       msg = msg + "\r\n -The certificate name does not match the authenticated name.\r\n"; 
       acceptCertificate = false; 
      } 

      //There is some other problem with the certificate 
      if ((sslPolicyErrors & 
       SslPolicyErrors.RemoteCertificateChainErrors) == SslPolicyErrors.RemoteCertificateChainErrors) 
      { 
       foreach (X509ChainStatus item in chain.ChainStatus) 
       { 
        if (item.Status != X509ChainStatusFlags.RevocationStatusUnknown && 
         item.Status != X509ChainStatusFlags.OfflineRevocation) 
         break; 

        if (item.Status != X509ChainStatusFlags.NoError) 
        { 
         msg = msg + "\r\n -" + item.StatusInformation; 
         acceptCertificate = false; 
        } 
       } 
      } 
     } 

     //If Validation failed, present message box 
     if (acceptCertificate == false) 
     { 
      msg = msg + "\r\nDo you wish to override the security check?"; 
      //   if (MessageBox.Show(msg, "Security Alert: Server could not be validated", 
      //      MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1) == DialogResult.Yes) 
      acceptCertificate = true; 
     } 

     return acceptCertificate; 
    } 

} 

이렇게 웹 요청을하기 전에 메소드를 호출하십시오. 우리가 (코드)를 볼 수 있다면 당신은 당신의 WebRequest 클래스를 만들고있어 어떻게

Certificates.Instance.GetCertificatesAutomatically(); 

또한 그것은 문제를 진단하는 데 도움이됩니다.

관련 문제