我知道这是很简单的一个证书添加到HttpWebRequest的。但是,我还没有找到一个方式做使用Web客户端等效的。基本上,我想使用的WebClient特定的证书发出一个POST。

您将如何使用Web客户端完成这个确切的代码:

    var request = (HttpWebRequest) WebRequest.Create("my-url");
    request.Method = "POST";
    request.ClientCertificates.Add(new X509Certificate()); //add cert
有帮助吗?

解决方案

您必须继承和覆盖一个或多个功能。

class MyWebClient : WebClient
{
    protected override WebRequest GetWebRequest(Uri address)
    {
        HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address);
        request.ClientCertificates.Add(new X509Certificate());
        return request;
    }
}

其他提示

public class CertificateWebClient : WebClient
{
    private readonly X509Certificate2 certificate;

    public CertificateWebClient(X509Certificate2 cert)
    {
        certificate = cert;
    }

    protected override WebRequest GetWebRequest(Uri address)
    {
        HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address);

        System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate(Object obj, X509Certificate X509certificate, X509Chain chain, System.Net.Security.SslPolicyErrors errors)
        {
            return true;
        };

        request.ClientCertificates.Add(certificate);
        return request;
    }
}

现在你可以用自签名的证书! (“基础连接已关闭:无法建立用于SSL / TLS安全通道信任关系;基础连接已关闭:无法建立用于SSL / TLS安全通道的信任关系;”)

        X509Certificate2 Cert = new X509Certificate2("client.p12", "1234", X509KeyStorageFlags.MachineKeySet);

        // Create a new WebClient instance.
        CertificateWebClient myWebClient = new CertificateWebClient(Cert);

        string fileName = Installation.destXML;
        string uriString = "https://xxxxxxx.xx:918";
        // Upload the file to the URI.
        // The 'UploadFile(uriString,fileName)' method implicitly uses HTTP POST method.
        byte[] responseArray = myWebClient.UploadFile(uriString, fileName);

        // Decode and display the response.
        Console.WriteLine("\nResponse Received.The contents of the file uploaded are:\n{0}",
            System.Text.Encoding.ASCII.GetString(responseArray));

只要继承WebClient,添加自己的ClientCertificates财产和重写WebClient.GetWebRequest(System.Uri)方法。我没有时间去这个从VB转换为C#,但它应该是相当不言自明:

Imports System.Net

Public Class WebClient2
    Inherits System.Net.WebClient

    Private _ClientCertificates As New System.Security.Cryptography.X509Certificates.X509CertificateCollection
    Public ReadOnly Property ClientCertificates() As System.Security.Cryptography.X509Certificates.X509CertificateCollection
        Get
            Return Me._ClientCertificates
        End Get
    End Property
    Protected Overrides Function GetWebRequest(ByVal address As System.Uri) As System.Net.WebRequest
        Dim R = MyBase.GetWebRequest(address)
        If TypeOf R Is HttpWebRequest Then
            Dim WR = DirectCast(R, HttpWebRequest)
            If Me._ClientCertificates IsNot Nothing AndAlso Me._ClientCertificates.Count > 0 Then
                WR.ClientCertificates.AddRange(Me._ClientCertificates)
            End If
        End If
        Return R
    End Function
End Class

在安装上我们的前端,新证书的,有趣的事情发生了。我们开始收到错误:

“基础连接已关闭:无法建立用于SSL / TLS安全通道;基础连接已关闭信任关系:无法建立信任关系的SSL / TLS安全通道;”

我们通过将每个前端和打开浏览器照顾错误的。看来,IE被缓存旧的证书。通过打开浏览器,新证书生效。问题解决!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top