static bool RemoteCertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
if (sslPolicyErrors == SslPolicyErrors.None)
return true;
WebRequest request = sender as WebRequest;
if (request == null || certificate == null)
return false;
bool isAllowed;
string allowed, urn = String.Format("{0}:{1}", request.RequestUri.Host, request.RequestUri.Port);
if (Storage.Read(urn, certificate.GetCertHashString(), out allowed) && bool.TryParse(allowed, out isAllowed))
return isAllowed;
string sslErrorDesc = String.Empty;
if ((sslPolicyErrors & SslPolicyErrors.RemoteCertificateChainErrors) == SslPolicyErrors.RemoteCertificateChainErrors)
sslErrorDesc += Resources.RemoteCertificateChainErrors;
else if ((sslPolicyErrors & SslPolicyErrors.RemoteCertificateNameMismatch) == SslPolicyErrors.RemoteCertificateNameMismatch)
sslErrorDesc += Resources.RemoteCertificateNameMismatch;
else if ((sslPolicyErrors & SslPolicyErrors.RemoteCertificateNotAvailable) == SslPolicyErrors.RemoteCertificateNotAvailable)
sslErrorDesc += Resources.RemoteCertificateNotAvailable;
sslErrorDesc = String.Format(Resources.SslErrorDescPromptFormat,
urn, sslErrorDesc, certificate.Subject, certificate.GetCertHashString());
DialogResult dr = MessageBox.Show(sslErrorDesc, "SSL Error", MessageBoxButtons.YesNo, MessageBoxIcon.Error, MessageBoxDefaultButton.Button2);
if (dr != DialogResult.Yes)
return false;
Storage.Write(urn, certificate.GetCertHashString(), true.ToString());
return true;
}