0

I’m calling a web service from my C# program. It works when I run it from my PC. When I run it on a server, I get the following error:

Error          2021-10-29 03:40:41 System.Net.WebException: The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel. ---> System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure.

Even though it works on my PC, I decided to change the following procedure which val;idates whether the certificate is valid or not and returns true if it is.

    private static bool ValidateRemoteCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors policyErrors)
    {
        DateTime expirationDate = Convert.ToDateTime(certificate.GetExpirationDateString());

        if (expirationDate < DateTime.Now)
        {
            return false; //EXPIRED;            
        }

        if (policyErrors != SslPolicyErrors.None)
        {
            return false;            
        }

        return true;
    }

I changed it to always return true.

    private static bool ValidateRemoteCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors policyErrors)
    {
        DateTime expirationDate = Convert.ToDateTime(certificate.GetExpirationDateString());

        if (expirationDate < DateTime.Now)
        {
            return true; //<<---return true = "cert if good" to see if the issue goes away
            //return false; //EXPIRED; //Is this the problem?
        }

        if (policyErrors != SslPolicyErrors.None)
        {
            return true; //<<---return true = "cert if good" to see if the issue goes away
            //return false; //Cert has errors, return false for validate outcome
        }

        return true;
    }

But I got the same error.

I am using TLS12:

System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12; 

Why would it when running on my PC but not on a server?

Chad
  • 22,004
  • 48
  • 182
  • 307

0 Answers0