2013-02-28 4 views
0

많은 URL에 연결하고 특정 스레드에서만 SSL 인증서를 검사해야하는 멀티 스레드 응용 프로그램이 있습니다.현재 스레드에서만 서버/SSL 인증서 검사 (.NET)

나는 ServicePointManager.ServerCertificateValidationCallback을 사용할 수 있지만 동시에 비동기 모드와 모든 스레드에서 작동한다는 것을 알고 있습니다.

URL에 연결하는 함수의 동기 실행 내에서 현재 스레드에서 수행 할 검사가 필요합니다.

아이디어가 있으십니까?

답변

1

당신과 같이 요청 및 인증서 기능 사이의 매핑을 정의 할 수 있습니다 :

// delegate definition for cert checking function 
private delegate bool CertFunc(X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors); 

// mapping between outbound requests and cert checking functions 
private static readonly ConcurrentDictionary<HttpWebRequest, CertFunc> _certFuncMap = new ConcurrentDictionary<HttpWebRequest, CertFunc>(); 

// global cert callback 
private static bool ServerCertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) 
{ 
    // call back into the cert checking function that is associated with this request 
    var httpWebRequest = (HttpWebRequest)sender; 
    CertFunc certFunc = _certFuncMap[httpWebRequest]; 
    return certFunc(certificate, chain, sslPolicyErrors); 
} 

을 그런 요청을 만들고 코드에서 :

// register the global cert callback 
ServicePointManager.ServerCertificateValidationCallback += ServerCertificateValidationCallback; 

// create the request object 
var httpWebRequest = (HttpWebRequest)WebRequest.Create(requestUri); 

// cert checking function 
CertFunc certFunc = (certificate, chain, sslPolicyErrors) => 
{ 
    // perform cert logic here 
    return true; 
}; 
_certFuncMap[httpWebRequest] = certFunc; 

using (var webResponse = httpWebRequest.GetResponse()) 
{ 
    // process the response... 
} 

// clean up the mapping 
_certFuncMap.TryRemove(httpWebRequest, out certFunc); 
+0

놀라운, 감사합니다! – Igorek