Thursday, April 4, 2013

OData: attach a client certificate through code when connecting to an OData service

At a customer, we had to use a 3th party OData service. Using OData feeds in .NET is simple, but this one was a little bit harder to use because it was secured using a client certificate.

So how can we tell OData to include the certificate when connecting to the service?
  • Open or create a Visual Studio project where you want to use the OData feed.
  • Add a service reference to the OData feed using the Visual Studio Add Service Reference option. If the metadata url is also secured, you can use Fiddler to temporarily work around the security.
  • A reference is generated for you and added to the Visual Studio project.
  • Inside this reference you’ll find a partial class containing a DataServiceContext object:
  • To include our client certificate, we can extend the generated class with some extra code. Therefore add a partial.cs file in the same namespace:
  • Now we can create the DataServiceContext and include the certificate using the property we just added:

No comments: