Saturday, February 18, 2012

ASP.NET Web API HttpClient and Client Certificates

I started working with WCF Web API (now released as ASP.NET Web API) and one thing that wasn't obvious is how to add a client certificate to the client, since the process is not the same as HttpWebRequest.

In order to do that, one needs to instantiate a WebRequestHandler, set the client certificate there, and use that to create the HttpClient.
WebRequestHandler handler = new WebRequestHandler();
X509Certificate2 certificate = GetMyX509Certificate();
handler.ClientCertificates.Add(certificate);
HttpClient client = new HttpClient(handler);
Post a Comment