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);
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With