質問

私はHttpWebRequestのに証明書を追加する非常に単純です知っています。しかし、私は、Webクライアントを使用して同等の操作を行うための方法を発見していません。 Basiclyは、私は、Webクライアントを使用して、特定の証明書を使用してPOSTを送信します。

どのようにWebクライアントを使用してこの正確なコードを達成なります:

    var request = (HttpWebRequest) WebRequest.Create("my-url");
    request.Method = "POST";
    request.ClientCertificates.Add(new X509Certificate()); //add cert
役に立ちましたか?

解決

あなたは、サブクラス化して、1つ以上の機能をオーバーライドする必要があります。

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