How to connect certificate to C # client for ODATA service?

We have an ODATA service that is protected using certificates. We use AddWebReference to get proxies in our C # code.

Is there a way to bind a certificate that is in our certificate store to this generated proxy class?

We can add a certificate using HTTPClient, but we would like to avoid using HTTPClientto talk to our ODATA service and prefer to use the AddWebReference method.

+3
source share
1 answer

I think this is probably already 3 years for you, but I hope it will help someone.

, .

ClientCertificate , -, . , , :

public partial class YourContainer
{
    private X509Certificate clientCertificate = null;

    public X509Certificate ClientCertificate
    {
        get
        {
            return clientCertificate;
        }
        set
        {
            if (value == null)
            {
                // if the event has been hooked up before, we should remove it
                if (clientCertificate != null)
                    this.SendingRequest -= this.OnSendingRequest_AddCertificate;
            }
            else
            {
                // hook up the event if its being set to something non-null
                if (clientCertificate == null)
                    this.SendingRequest += this.OnSendingRequest_AddCertificate;
            }

            clientCertificate = value;
        }
    }

    private void OnSendingRequest_AddCertificate(object sender, SendingRequestEventArgs args)
    {
        if (null != ClientCertificate)
        {
            ((HttpWebRequest)args.Request).ClientCertificates.Add(ClientCertificate);
        }
    }
}

ClientCertificate :

// Get the store where your certificate is in.
var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);

store.Open(OpenFlags.ReadOnly);

// Select your certificate from the store (any way you like).
X509Certificate2Collection certColl = store.Certificates.Find(X509FindType.FindByThumbprint, yourThumbprint, false);

// Set the certificate property on the container.
container.ClientCertificate = certColl[0];

store.Close();

WCF DataServices 5+ ( )

, WCF Dataservice 5+, SendingRequest , [Obsolete("SendingRequest2 has been deprecated in favor of SendingRequest2.")] ( ;)). , , , SendingRequest2, :

public partial class YourContainer
{
    private X509Certificate clientCertificate = null;

    public X509Certificate ClientCertificate
    {
        get
        {
            return clientCertificate;
        }
        set
        {
            if (value == null)
            {
                // if the event has been hooked up before, we should remove it
                if (clientCertificate != null)
                    this.SendingRequest2 -= this.OnSendingRequest_AddCertificate;
            }
            else
            {
                // hook up the event if its being set to something non-null
                if (clientCertificate == null)
                    this.SendingRequest2 += this.OnSendingRequest_AddCertificate;
            }

            clientCertificate = value;
        }
    }

    private void OnSendingRequest_AddCertificate(object sender, SendingRequest2EventArgs args)
    {
        if (null != ClientCertificate)
        {
            ((HttpWebRequestMessage)args.RequestMessage).HttpWebRequest.ClientCertificates.Add(ClientCertificate);
        }
    }
}

- ( , SendingRequest SendingRequest2).

, , , args.RequestMessage HttpWebRequestMessage, InvalidCastExceptions. , . , . InternalODataRequestMessage requestMessage ODataBatchOperationRequestMessage. , .

, , , .

0

Source: https://habr.com/ru/post/1778447/


All Articles