2017-11-11 4 views
0

내 문제는 특히 .net 표준 2.0에 있습니다. 동일한 코드가 .net 프레임 워크에서 작동하는 것 같아서 완전히 확신 할 수 없습니다..net 표준 2.0의 자체 서명 된 인증서

문제는 자체 서명 된 인증서를 사용하는 서버에 http 요청을 보내려한다는 것입니다. 이제 .NET 프레임 워크 (특히 4.6.1)에서이 문제를 해결하는 방법은 다음을 사용하는 것입니다.

ServicePointManager.ServerCertificateValidationCallback = CustomValidation; 

public static bool CustomValidation 
      (object sender, 
      X509Certificate certificate, 
      X509Chain chain, 
      SslPolicyErrors policyErrors) 
     { 
      return true; 
     } 

그리고이 문제가 해결되었습니다. 그러나 .net 표준 이렇게 컴파일하는 것처럼 보이지만 동일한 오류 (WinHttpException - 보안 오류가 발생했습니다) System.AggregateException이 발생했습니다. HResult = 0x80131500 메시지 = 하나 이상의 오류가 발생했습니다. (요청을 보내는 동안 오류가 발생했습니다.) 소스 : StackTrace : at System.Threading.Tasks.Task`1.GetResultCore (부울 waitCompletionNotification) at matrix_tester.Program.Main (String [] args) in C : \ Users \ Nick \ source \ repos \ matrix-tester \ Program.cs : 줄 11

내부 예외 1 : HttpRequestException : 요청을 보내는 동안 오류가 발생했습니다.

내부 예외 2 : WinHttpException : 보안 오류가 종료 여기 내 기지에서

난 발생했습니다. ServicePointManager가 .net 표준에서 사용되지 않습니까?

+0

.NET 표준 2.0을 사용하여 자체 서명 된 인증서를 무시할 수있는 해결책을 찾았습니까? –

답변

0

ServicePointManager는 2.0에서 사용할 수 있어야합니다.

면책. 왜 코드가 작동하지 않는지 모르겠다. 인증서를 자동으로 받아 들일 필요가있을 때 항상 사용하는 해킹이 있습니다. 그것은 2.0에서 작동합니다. 그러나이 스크립트는 보안 위반 인 모든 자체 서명 인증서를 수락한다는 것을 기억하십시오. 귀하의 재량에 따라 사용하십시오. 싱글 톤 클래스입니다. 다음과 같이 프로그램 시작시이를 호출하십시오.

Certificates.Instance.GetCertificatesAutomatically(); 

그리고 프로그램 전체에서 작동해야합니다. 그것이 당신을 도울 수 있기를 바랍니다.

using System; 
using System.Collections.Generic; 
using System.Security; 
using System.Net; 
using System.Security.Cryptography.X509Certificates; 
using System.Security.Cryptography; 
using System.Net.Security; 

namespace test 
{ 
    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; 
     } 

    } 
} 
+0

ServicePointManager.ServerCertificateValidationCallback + = 새로운 RemoteCertificateValidationCallback ((보낸 사람, 인증서, 체인, policyErrors) => {return true;})이 동작하지 않는 것 같습니다 (이유를 볼 수는 없지만). 나에게 효과가있는 것 같지 않다. –

+0

위임장에 중단 점을 넣고있다. –