I run my program in standard user privilege and I assume the API ValidateServerCertificate
uses the root-certificates from the local user certificate store to validate the SSL certificate.
Code sample from the C#.
// The following method is invoked by the RemoteCertificateValidationDelegate.
public static bool ValidateServerCertificate(
object sender,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors sslPolicyErrors)
{
if (sslPolicyErrors == SslPolicyErrors.None)
return true;
Console.WriteLine("Certificate error: {0}", sslPolicyErrors);
// Do not allow this client to communicate with unauthenticated servers.
return false;
}
Requirement: I need the SSL certificate validation based upon the root certificates present in the local machine certificate store.
I came up with a logic to to compare the thumbprint of the provided Root – SSL certificate with the ones present in the local machine certificate store.
X509Store store = new X509Store("Root", StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
X509ChainElement rootElement = chain.ChainElements[chain.ChainElements.Count - 1]; //To get the root of the chain
X509Certificate2 rootCert = rootElement.Certificate;
foreach (X509Certificate2 cert in store.Certificates)
{
// Compare the thumbprint of each certificate
if (cert.Thumbprint.Equals(rootCert.GetCertHashString(), StringComparison.OrdinalIgnoreCase))
{
// Certificate found
return true;
}
}
return false;
But is there a better way to perform this ? I am using .NET-FW Version 4.
3
You can try some other way:
Export your certificate (Private key) in .pfx format (with password) and keep it in some folder location.
To export your certificate, you can also use DigiCert Utility, which can be downloaded from the below link:
var cert_Path = "D:YourFolderLocationYourCertificate.pfx";
var cert = new X509Certificate2(cert_Path, "Certificate_Password", X509KeyStorageFlags.MachineKeySet);
Now, try your code with this.