【问题标题】:How to validate a certificate?如何验证证书?
【发布时间】:2014-02-27 17:41:18
【问题描述】:

我刚刚开始一个项目,我需要使用 FTPS 连接到第 3 方服务器。他们要为我们提供他们的证书我还没有,所以我要创建我们自己的开发环境来开始。我使用 Alex FTPS 客户端作为我的起点 (http://ftps.codeplex.com/)...但我也愿意将 WinSCP 用作不同的客户端。

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

目前已完成的步骤

  1. 安装并配置 FileZilla 服务器

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

3。在 Alex FTPS 客户端中,他们有一个带有此签名的回调

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

我的(各种问题)是:

  1. 该方法应该包含什么内容?

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

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

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

感谢您的任何提示和指示

【问题讨论】:

    标签: c# validation certificate ftps


    【解决方案1】:

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

    1) 已安装 FileZilla 服务器

    • 用它来创建自己的自签名证书
    • 菜单:设置 | 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); 
    

    .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;
          }
        }
    

    希望对人们有所帮助。

    【讨论】:

    • 但是 _client 是一个 TcpClient 权限。如果是这样,连接没有这些可能会过载。 string message = _client.Connect(hostname, port, credential, ESSLSupportMode.Implicit, null, // new RemoteCertificateValidationCallback(ValidateTestServerCertificate), null, 0, 0, 0, null);
    猜你喜欢
    • 2016-09-07
    • 2015-08-17
    • 2011-04-06
    • 2022-01-09
    • 1970-01-01
    • 2021-07-22
    • 2016-09-22
    • 2015-12-18
    相关资源
    最近更新 更多