Https Certificate Validation

S Abijith 471 Reputation points
2021-09-20T09:38:49.643+00:00

Hi All,
We have a WPF application built on .Net Framework 4.7 which acts as an HTTPS client. We are currently trying to use JSON-RPC to connect to the server to download the files.
We are currently not validating the HTTPS certificate. But now we need to validate the certificate to verify the authenticity.

Can anyone please let us know as to how we can validate the certificate and what parameters are to be checked for validation.

Any help would be appreciated!!
Thank you!

Developer technologies | C#
{count} votes

Accepted answer
  1. Jack J Jun 25,296 Reputation points
    2021-09-21T02:09:21.07+00:00

    @S Abijith , after you installed the certificate in the website, you could try to use HttpWebRequest.ServerCertificateValidationCallback Property to set a callback function to validate the server certificate.

    Here is a code example you could refer to.

     static void Main(string[] args)  
                {  
                    HttpWebRequest request = WebRequest.CreateHttp("url");  
                    request.ServerCertificateValidationCallback += ValidateServerCertificate;  
                }  
          
          
                public static bool ValidateServerCertificate(  
          object sender,  
          X509Certificate certificate,  
          X509Chain chain,  
          SslPolicyErrors sslPolicyErrors)  
            {  
                if (sslPolicyErrors == SslPolicyErrors.None)  
                {  
                    Console.WriteLine("Certificate is valid");  
                    return true;  
                   
                }  
                else  
                {  
                    Console.WriteLine("Certificate error: {0}", sslPolicyErrors);  
      
                    // Do not allow this client to communicate with unauthenticated servers.  
                    return false;  
                }  
            }  
    

    Best Regards,
    Jack


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.