Joh*_*ith 7 ssl certificate ftpwebrequest
我有一个.NET客户端应用程序,尝试将文件ftp到ftp站点,该站点具有自签名SSL证书.此ftp站点在Windows 7 Enterprise,IIS 7上运行.我收到以下错误:"远程证书根据验证过程无效".
我已尝试在受信任的根证书中安装证书,但仍然无效.
我在代码中使用了委托回调,这里提到了一些帖子 - 它有效.但我不想在我的生产代码中使用它.
同样在生产中,我们的一些客户使用自签名证书.
有关如何解决此问题的任何想法?
Mar*_*ryl 10
@Luca 投票最多的答案盲目接受任何证书。这是一个安全缺陷。
在实现ServicePointManager.ServerCertificateValidation回调时,应该验证证书。例如,通过根据已知值检查证书的哈希值:
using System.Net;
using System.Net.Security;
using System.Security.Cryptography;
Run Code Online (Sandbox Code Playgroud)
ServicePointManager.ServerCertificateValidationCallback +=
(sender, certificate, chain, errors) =>
{
return
(errors == SslPolicyErrors.None) ||
certificate.GetCertHashString(HashAlgorithmName.SHA256).Equals(
"EB8E0B28AE064ED58CBED9DAEB46CFEB3BD7ECA677...");
};
Run Code Online (Sandbox Code Playgroud)
对于X509Certificate.GetCertHashString需要的重载HashAlgorithmName.SHA256,您需要 .NET 4.8。在旧版本上,使用返回 SHA-1 哈希值的无参数重载。
基于当您知道无效证书是安全时测试 X509Certificate.Thumbprint 属性是否安全?
有关代码的 VB.NET 版本,请参阅在 VB.NET 中接受自签名 TLS/SSL 证书。
小智 5
您必须覆盖证书检查,以便始终将其视为良好。这不会阻止通道保持SSL保护。
Uri target = new Uri("ftp://yourUri");
string fileName = @"fullPathOfYourFile";
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(target);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential("user", "password");
request.EnableSsl = true;
//overwrite the certificate checks
ServicePointManager.ServerCertificateValidationCallback =
(s, certificate, chain, sslPolicyErrors) => true;
// Copy the contents of the file to the request stream
//....
Run Code Online (Sandbox Code Playgroud)
小智 0
我通过 .NET 遇到了同样的问题,并且证书根和链受到我的帐户甚至本地计算机帐户的信任。所以证书是金色的。
对我来说,我使用了错误的主机名。我使用的是完全限定的名称(并且到达了正确的位置),但证书实际上颁发给了不同的别名。因此,请确保您的服务器名称与证书上的名称完全匹配。
查看这篇文章,这就是我找到答案的方法...也许事件订阅也是您所需要的...
http://www.limilabs.com/blog/the-remote-certificate-is-invalid-according-to-the-validation-procedure
| 归档时间: |
|
| 查看次数: |
7839 次 |
| 最近记录: |