How to validate a certificate?

删除回忆录丶 提交于 2019-12-04 08:44:16

Gee, no answers... so I will answer my own question and it might be helpful for others. Here are my steps, not sure if everyone is needed, but this is what I ended up doing.

1) Installed FileZilla Server

  • Used it to create its own self-signed certificate
  • menu: Settings | SSL/TSL Settings | Generate New Certificate
  • enter in the appropriate values
  • ensuring I had the common name = server address correct.
  • this generated a certificate with private key in the .crt extension/format

2) As I was on Windows, I found I couldn't install this certificate in the certificate store, so the extra step was I needed to convert it first

3) Launch windows MMC Snap-in Console

  • install the certificate into the Computer Account, Trusted Root Certification Authorities store

4) In my code (in FTPS library, in this case Alex FTPS My connection looks like this:

var credential = new NetworkCredential(username, password);
string message = _client.Connect(hostname, port, credential, 
    ESSLSupportMode.Implicit,
    null, // new RemoteCertificateValidationCallback(ValidateTestServerCertificate), 
    null, 0, 0, 0, null); 

The .net/Windows infrastructure plumbing handles all validation for me already

5) But if you wanted custom validation, or if you didn't want to install the certificate in the windows store, you can use this sample code here: http://msdn.microsoft.com/en-us/library/office/dd633677%28v=exchg.80%29.aspx

private static bool ValidateTestServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
    {
      // If the certificate is a valid, signed certificate, return true.
      if (sslPolicyErrors == System.Net.Security.SslPolicyErrors.None)
      {
        return true;
      }

      // If there are errors in the certificate chain, look at each error to determine the cause.
      if ((sslPolicyErrors & System.Net.Security.SslPolicyErrors.RemoteCertificateChainErrors) != 0)
      {
        if (chain != null && chain.ChainStatus != null)
        {
          foreach (System.Security.Cryptography.X509Certificates.X509ChainStatus status in chain.ChainStatus)
          {
            if ((certificate.Subject == certificate.Issuer) &&
               (status.Status == System.Security.Cryptography.X509Certificates.X509ChainStatusFlags.UntrustedRoot))
            {
              // Self-signed certificates with an untrusted root are valid. 
              continue;
            }
            else
            {
              if (status.Status != System.Security.Cryptography.X509Certificates.X509ChainStatusFlags.NoError)
              {
                // If there are any other errors in the certificate chain, the certificate is invalid,
             // so the method returns false.
                return false;
              }
            }
          }
        }

        // When processing reaches this line, the only errors in the certificate chain are 
    // untrusted root errors for self-signed certificates. These certificates are valid
    // for default Exchange server installations, so return true.
        return true;
      }
      else
      {
     // In all other cases, return false.
        return false;
      }
    }

Hope that helps people.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!