如何验证证书?

Ray*_*ond 5 c# validation certificate ftps

我刚刚开始一个项目,我将需要使用FTPS连接到第三方服务器.他们将向我们提供我尚未拥有的证书,因此我将创建自己的开发环境以便开始使用.我使用Alex FTPS客户端作为我的起点(http://ftps.codeplex.com/)...但我也愿意将WinSCP用作其他客户端.

我的问题如下: - 如何在.Net中验证证书?

已完成的步骤

  1. 已安装和配置的FileZilla Server

  2. 使用FileZilla创建了自签名证书

3.在Alex FTPS客户端中,他们使用此签名进行回调

private static bool ValidateTestServerCertificate(
    object sender, 
    X509Certificate certificate, 
    X509Chain chain, 
    SslPolicyErrors sslPolicyErrors)
{
    // what goes here?
}
Run Code Online (Sandbox Code Playgroud)

我的(各种问题)是:

  1. 假设进入该方法是什么?

  2. 我只对证书有一点了解.FileZilla自生成证书甚至是X509证书吗?我必须承认,我不知道不同类型的证书格式.

  3. 我写什么类型的代码?当我在WCF或HTTP中使用SSL时,已经为我处理了很多管道.这不是FTPS的情况吗?即我将使用Windows中的MMC管理单元导入或安装证书.

  4. 如果我没有在Alex FTPS中实现Callback方法,我收到消息:"远程证书根据验证程序无效"

感谢您提供任何提示和指示

Ray*_*ond 7

哎呀,没有答案......所以我会回答我自己的问题,这可能对其他人有帮助.这是我的步骤,不确定是否需要每个人,但这是我最终做的.

1)安装FileZilla Server

  • 用它来创建自己的自签名证书
  • 菜单:设置| SSL/TSL设置| 生成新证书
  • 输入适当的值
  • 确保我的公共名称=服务器地址正确.
  • 这会生成一个带有.crt扩展名/格式私钥的证书

2)因为我在Windows上,我发现我无法在证书存储中安装此证书,所以额外的步骤是我需要先转换它

3)启动Windows MMC管理单元控制台

  • 将证书安装到计算机帐户,受信任的根证书颁发机构存储中

4)在我的代码中(在FTPS库中,在这种情况下,Alex FTPS我的连接看起来像这样:

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

.net/Windows基础架构管道已经为我处理了所有验证

5)但是如果你想要自定义验证,或者如果你不想在windows商店中安装证书,你可以在这里使用这个示例代码: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;
      }
    }
Run Code Online (Sandbox Code Playgroud)

希望能帮助别人.