Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you send a certificate with an HttpRequestMessage?

Here I have an HttpRequestMessage, and I am trying to add a client certificate to it, but cannot seem to find how to do this. 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);
like image 570
KateMak Avatar asked Jun 10 '16 14:06

KateMak


People also ask

How do I pass client certificate in curl?

Make a request from Curl using mutual TLS The CA root certificate will be used to verify that the client can trust the certificate presented by the server. Pass your certificate, private key, and root CA certificate to curl to authenticate your request over TLS.

What is client side certificate?

Client Certificates are digital certificates for users and individuals to prove their identity to a server. Client certificates tend to be used within private organizations to authenticate requests to remote servers.


1 Answers

Here is an answer combining HttpClient with HttpRequestMessage.

The HttpRequestMessage holding the data and client handling how the data is sent.

WebRequestHandler handler = new WebRequestHandler();
X509Certificate 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();

Edit: Here is a link explaining the difference between WebRequestHandler, HttpClientHandler & HttpClient to understand which one you should use when: https://docs.microsoft.com/en-us/archive/blogs/henrikn/httpclient-httpclienthandler-and-webrequesthandler-explained

like image 139
misha130 Avatar answered Oct 21 '22 03:10

misha130