당신과 같이 요청 및 인증서 기능 사이의 매핑을 정의 할 수 있습니다 :
// 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);
놀라운, 감사합니다! – Igorek