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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace ODataSample | |
{ | |
/// <summary> | |
/// There are no comments for Entities in the schema. | |
/// </summary> | |
public partial class Entities : global::System.Data.Services.Client.DataServiceContext | |
{ | |
/// <summary> | |
/// Initialize a new Entities object. | |
/// </summary> | |
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")] | |
public Entities(global::System.Uri serviceRoot) : | |
base(serviceRoot) | |
{ | |
this.ResolveName = new global::System.Func<global::System.Type, string>(this.ResolveNameFromType); | |
this.ResolveType = new global::System.Func<string, global::System.Type>(this.ResolveTypeFromName); | |
this.OnContextCreated(); | |
} | |
partial void OnContextCreated(); | |
} | |
} |
- 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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace ODataSample | |
{ | |
public partial class Entities | |
{ | |
private X509Certificate _clientCertificate = null; | |
public X509Certificate ClientCertificate | |
{ | |
get | |
{ | |
return _clientCertificate; | |
} | |
set | |
{ | |
if (value == null) | |
{ | |
if (_clientCertificate != null) | |
this.SendingRequest -= OnSendingRequest_AddCertificate; | |
} | |
else | |
{ | |
if (_clientCertificate == null) | |
this.SendingRequest += OnSendingRequest_AddCertificate; | |
} | |
} | |
_clientCertificate = value; | |
} | |
private void OnSendingRequest_AddCertificate(object sender, SendingRequestEventArgs args) | |
{ | |
if (null != ClientCertificate) | |
(args.Request as HttpWebRequest).ClientCertificates.Add(ClientCertificate); | |
} | |
} | |
} |
- Now we can create the DataServiceContext and include the certificate using the property we just added:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Entities ctx = new Entities(new Uri(settings.Url)); | |
ctx.ClientCertificate = new X509Certificate(settings.CertificatePath); |