9

인증서 인증을 사용해야하는 것은 처음입니다. 상업용 파트너는 XML Web Service와 HTTP 서비스라는 두 가지 서비스를 제공합니다. .NET 클라이언트로 두 가지 모두에 액세스해야합니다. 나는 (루트와 두 개의 중간에서) SSLCACertificates를 설치 한인증서 인증을 사용하여 웹 서비스 및 HTTP 인터페이스에 액세스

환경과 내 로컬 컴퓨터에서 클라이언트 인증서를 설정

0을 시도 무엇

(7 프로 우승) certmgr.exe를 사용합니다. 웹 서비스

  • 를 들어

    1.

    나는 클라이언트 인증서 (DER)가 있습니다.
  • 서비스는 .NET 프록시를 통해 소비됩니다. 마지막 문장에서보고

    OrderWSService proxy = new OrderWSService(); 
    string CertFile = "ClientCert_DER.cer"; 
    
    proxy.ClientCertificates.Add(new System.Security.Cryptography.X509Certificates.X509Certificate(CertFile)); 
    orderTrackingTO ot = new orderTrackingTO() { order_id = "80", tracking_id = "82", status = stateOrderType.IN_PREPARATION }; 
    resultResponseTO res = proxy.insertOrderTracking(ot); 
    

    예외 : The request failed with an empty response

여기에 코드입니다. HTTP 인터페이스

  • 를 들어

    2. 그것은 내가 POST 메소드를 통해 전화를 가지고 HTTPS 인터페이스입니다.

  • HTTPS 요청은 HTTPWebRequest를 사용하여 .NET 클라이언트에서 전송됩니다. 마지막 문장에서보고

    string PostData = "MyPostData"; 
    
    //setting the request 
    HttpWebRequest req; 
    req = (HttpWebRequest)HttpWebRequest.Create(url); 
    req.UserAgent = "MyUserAgent"; 
    req.Method = "POST"; 
    req.ContentType = "application/x-www-form-urlencoded"; 
    req.ClientCertificates.Add(new System.Security.Cryptography.X509Certificates.X509Certificate(CertFile, "MyPassword")); 
    
    //setting the request content 
    byte[] byteArray = Encoding.UTF8.GetBytes(PostData); 
    Stream dataStream = req.GetRequestStream(); 
    dataStream.Write(byteArray, 0, byteArray.Length); 
    dataStream.Close(); 
    
    //obtaining the response 
    WebResponse res = req.GetResponse(); 
    r = new StreamReader(res.GetResponseStream()); 
    

    예외 : The request was aborted: Could not create SSL/TLS secure channel

여기에 코드입니다.

3. 마지막 시도 :

Error 107 (net::ERR_SSL_PROTOCOL_ERROR) 

내가 끼 었어 : 나는 두 URL에 액세스하려고하면 크롬 브라우저

를 사용하여이 인증서를 설치 한 후, 나는 107 오류가 발생합니다.

+1

U는 SSL 인증서 athorization와 HTTP 요청을 할 수 없습니다. https를 통해해야합니다. 이 인증서가 필요하십니까? 대답이 예인 경우 https를 통해이 웹 서비스를 통신해야합니다. – harry180

+0

@ harry180. 명확히 해 주셔서 감사합니다. 나는 그 질문을 편집했다. –

+0

http 프로토콜에서 사용자 이름과 암호를 보내면 MD5에서 최소 암호화되어야합니다. 그렇게해도 권한이없는 사람에게이 정보가 유출되는 것은 쉽습니다. 그런 종류의 정보 공유를 위해 https 프로토콜을 다시 고려하십시오. – harry180

답변

5

다음은 SSL 연결을 테스트하는 두 가지 방법과 사이트를 테스트하는 두 가지 방법을 보여줍니다. 다른 하나는 SSL이 실패한 이유를 확인하는 콜백 메서드입니다. 그 밖의 것이 없다면 왜 실패했는지 더 잘 이해할 수 있습니다.

이 메서드를 호출하면 cert 저장소에서 자동으로 읽으려는 인증서에 대한 선택 대화 상자가 나타납니다. 인증서를 설치하지 않은 이유는 올바른 인증서가없는 경우 문제가 인증서 설치 방법과 일치하기 때문입니다.

할 수있는 가장 좋은 것은 간단한 콘솔 응용 프로그램에서이 코드를 넣어 :

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

private static void CheckSite(string url, string method) 
{ 
    X509Certificate2 cert = null; 
    ServicePointManager.ServerCertificateValidationCallback += ValidateRemoteCertificate; 

    X509Store store = new X509Store(StoreLocation.LocalMachine); 
    store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly); 
    X509Certificate2Collection certcollection = (X509Certificate2Collection)store.Certificates; 
    // pick a certificate from the store 
    cert = X509Certificate2UI.SelectFromCollection(certcollection, 
      "Caption", 
      "Message", X509SelectionFlag.SingleSelection)[0]; 

    store.Close(); 

    HttpWebRequest ws = (HttpWebRequest)WebRequest.Create(url); 
    ws.Credentials = CredentialCache.DefaultCredentials; 
    ws.Method = method; 
    if (cert != null) 
     ws.ClientCertificates.Add(cert); 

    using (HttpWebResponse webResponse = (HttpWebResponse)ws.GetResponse()) 
    { 
     using (Stream responseStream = webResponse.GetResponseStream()) 
     { 
      using (StreamReader responseStreamReader = new StreamReader(responseStream, true)) 
      { 
       string response = responseStreamReader.ReadToEnd(); 
       Console.WriteLine(response); 
       responseStreamReader.Close(); 
      } 

      responseStream.Close(); 
     } 
     webResponse.Close(); 
    } 
} 

/// <summary> 
/// Certificate validation callback. 
/// </summary> 
private static bool ValidateRemoteCertificate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error) 
{ 
    // If the certificate is a valid, signed certificate, return true. 
    if (error == System.Net.Security.SslPolicyErrors.None) 
    { 
     return true; 
    } 

    Console.WriteLine("X509Certificate [{0}] Policy Error: '{1}'", 
     cert.Subject, 
     error.ToString()); 

    return false; 
} 
+0

고마워요. 나는 그것을 시도 할 것이다. –

관련 문제