Search code examples
c#.netvalidationverifyx509certificate2

X509Certificate2 chain verify(validation) in c#


I have three certificates and need to verify them with each other. 1 root certificate and 2 leaf certificate.

Trying with this code block:

        bool retVal = false;

        X509Chain chain = new X509Chain();

        chain.ChainPolicy.ExtraStore.Add(rootCertificate.X509);

        chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck;

        retVal = chain.Build(leafCertificate.X509);

        return retVal;

Im getting always false. Cannot find any different way on internet.

Thanks for your helps.


Solution

  • If you add untrusted certificate(s) to the chain you need to validate with the AllowUnknownCertificateAuthority flag. Try to add this

    chain.ChainPolicy.VerificationFlags = X509VerificationFlags.AllowUnknownCertificateAuthority;
    

    before you call chain.Build().