c# - 如何使用 c# 2.0 WebClient 忽略证书错误 - 没有证书

标签 c# ssl-certificate webclient

使用 Visual Studio 2005 - C# 2.0,System.Net.WebClient.UploadData(Uri address, byte[] data)视窗服务器 2003

所以这是代码的精简版本:

static string SO_method(String fullRequestString)
{
    string theUriStringToUse = @"https://10.10.10.10:443"; // populated with real endpoint IP:port
    string proxyAddressAndPort = @"http://10.10.10.10:80/"; // populated with a real proxy IP:port
    Byte[] utf8EncodedResponse; // for the return data in utf8
    string responseString; // for the return data in utf16

    WebClient myWebClient = new WebClient(); // instantiate a web client
    WebProxy proxyObject = new WebProxy(proxyAddressAndPort, true);// instantiate & popuylate a web proxy
    myWebClient.Proxy = proxyObject; // add the proxy to the client
    myWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); // stick some stuff in the header

    UTF8Encoding utf8Encoding = new UTF8Encoding(false);// create a utf8 encoding
    Byte[] utf8EncodedRequest = HttpUtility.UrlEncodeToBytes(fullRequestString, utf8Encoding); // convert the request data to a utf8 byte array

    try
    {
        utf8EncodedResponse = myWebClient.UploadData(theUriStringToUse, "POST", utf8EncodedRequest); // pass the utf8-encoded byte array
        responseString = utf8Encoding.GetString(utf8EncodedResponse); // get a useable string out of the response
    }
    catch (Exception e)
    {
        // some other error handling
        responseString = "<CommError><![CDATA[" + e.ToString() + "]]></CommError>";// show the basics of the problem
    }
    return responseString;// return whatever ya got
}

这是我得到的错误:

The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.



我没有太多控制权来查看请求发出时发生了什么。我被告知它正在到达正确的目的地,并且出现“证书错误”。这可能是因为我的请求中的 IP 地址与其解析到的 URL 之间存在字面不匹配。我有多个 IP 我应该循环使用,因此指定 URL 将不起作用。我没有附上证书 - 根据端点所有者的说法,我也不应该附上证书。根据“他们”,证书错误是“正常的,我应该忽略它”。

有问题的证书应该是我们服务器上“就在那里”的众多 verisign 证书之一。我看到的忽略证书错误的示例似乎都暗示请求者附加了特定的 x509 证书(我不是)。

我看了.net WebService, bypass ssl validation! which kinda-sorta 描述了我的问题 - 除了它也有点-sorta 没有,因为我不知道我应该引用哪个证书(如果有)。

有没有办法让我忽略错误而不实际知道/关心导致问题的证书?
  • 并且请 - child 手套,小词和“傻瓜”代码,因为我并不是一个重量级的击球手。
  • 此流量通过专用线路 - 所以我的理解是忽略证书错误并不像开放互联网流量那么大。
  • 最佳答案

    SSL 证书用于机器建立信任关系。如果您输入一个 IP 地址,并最终与另一个 IP 地址交谈,这听起来与 DNS 劫持安全故障相同,SSL 旨在帮助您避免这种情况——而且可能是您不想忍受的事情从他们”。

    如果您最终可能与多台机器交谈(理想情况下,他们会让您看起来像一台机器),您将需要为每台可能的机器提供证书以启动信任。

    要忽略信任(我只需要在开发场景中临时执行此操作),以下代码段可能对您有用,但我强烈建议您在使用之前考虑忽略信任的影响:

    public static void InitiateSSLTrust()
    {
        try
        {
            //Change SSL checks so that all checks pass
            ServicePointManager.ServerCertificateValidationCallback =
               new RemoteCertificateValidationCallback(
                    delegate
                    { return true; }
                );
        }
        catch (Exception ex)
        {
            ActivityLog.InsertSyncActivity(ex);
        }
    }
    

    关于c# - 如何使用 c# 2.0 WebClient 忽略证书错误 - 没有证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1301127/

    相关文章:

    security - ssl证书如何防止黑客克隆

    c# - 在 C# 中使用 WebClient.DownloadString 发送 POST

    c# - 选择具有不同别名和不同条件的同一列

    c# - asp.net c# 中 session 变量范围的范围是什么?

    c# - 关于线程池最大线程的说明

    c# - 如何使用进度回调将文件下载到字符串中?

    c# - 在单元测试中出现错误 "Provider com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl not found"但在主程序中却没有

    c# - 需要帮助确定要编写的单元测试

    python-3.x - 在 python 3.7 中使用维基百科 API 时出现证书错误

    ssl-certificate - 从 vertx 中的客户端证书中提取用户主体