Search code examples
c#wcfwcf-securityclient-certificates

How to use WCF service reference client with X.509 client certificate, which is only available as .pfx file?


Context

I have a tested working WCF service which is configured to client certificate authentication. Now I would like to call it and provide a client certificate. I would like to use client class which was generated when I added a service reference to my client project.

NOTE: I must load my X 509 client certificate from file or other stream, because the client will run on a PaaS environment, so I will have no access to any OS provided certificate store.

X509Certificate certificate = new X509Certificate(path, password);

Question

I know I can configure the client in the following way:

client.ClientCredentials.ClientCertificate.SetCertificate(
    StoreLocation.CurrentUser,
    StoreName.My,
    X509FindType.FindBySubjectName,
    "myCert");

Unfortunately there is no overload for SetCertificate which accepts a loaded X509Certificate instance.

How can I configure my client to use the loaded X509Certificate?


Solution

  • All you need it just:

    client.ClientCredentials.ClientCertificate.Certificate = certificate