Search code examples
wcfconfigurationwcf-securityx509certificatex509certificate2

How Can I Create An Endpoint Address In Code When I Have A Certificate Identity?


My WCF client config has this in it:

...
<identity>
   <certificate encodedValue="encoded data" />
</identity>
...

I don't want to have to use an App.Config file so I am attempting to recreate this in code, however I am unable to do so. I have tried converting this string from base 64 and passing it in as raw data for the X509Certificate2 class, if I do that I get a CryptographicException: "Cannot find the requested object".

I thought that this was maybe because the format was DER, but I am not sure if that is the case, and if it is I don't know how to convert this to DER format.

Does anyone know how I can load this with code?


Solution

  • Try something like this, using and EndpointIdentity when you are creating your EndpointAddress:

    X509Certificate2 certificate = X509Certificate2.CreateFromCertFile("r certificate file goes here") as X509Certificate2;
    EndpointAddress endpointAddress = new EndpointAddress(new URI("Your service URI goes here"), EndpointIdentity.CreateX509CertificateIdentity(certificate));
    

    The source for the above sample is: http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/c343c266-850e-4eec-9e6f-a42b9659527c/