How do you send a certificate with HttpRequestMessage?

Here I have an HttpRequestMessage, and I'm trying to add a client certificate to it, but cannot find how to do it. Has anyone out there done something like this?

HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "myapi/?myParm=" + aParm);
//Want to add a certificate to request - a .p12 file in my project
myAPIResponse res = await SendAndReadAsAsync<myAPIResponse>(request, aCancelToken);
+4
source share
1 answer

Here is the answer combining HttpClient with HttpRequestMessage.

HttpRequestMessage, which stores data and the client that processes data sending.

WebRequestHandler handler = new WebRequestHandler();
X509Certificate2 certificate = GetMyX509Certificate();
handler.ClientCertificates.Add(certificate);
HttpClient client = new HttpClient(handler);
var request = new HttpRequestMessage (HttpMethod.Get, "myapi/?myParm=" + aParm);
HttpResponseMessage response = await client.SendAsync (request);
response.EnsureSuccessStatusCode();
+6
source

All Articles