I am working on a Xamarin Forms project that needs to send out a Http request that includes a certificate for authentication.
I have the code working to get the certificate, which is a PFX, and pass it to my HttpClient but when I make the request I always get back a 403 error. The code below is how I am sending the message:
// Attach certifcate collection to handler. var handler = new HttpClientHandler(); handler.ClientCertificateOptions = ClientCertificateOption.Manual; handler.ClientCertificates.AddRange(GetCertificate()); // Construct client with handler. var client = new HttpClient(handler); // Make request. var result = client.GetAsync("urlgoeshere").GetAwaiter().GetResult(); var responsebody = result.Content.ReadAsStringAsync().GetAwaiter().GetResult();
I removed the real URL, but with the correct one it is always a 403 error.
I then tested this with a HttpWebRequest and I get through just fine, the code for that is as follows:
X509Certificate2Collection certificate = GetCertificate(); var httpRequest = (HttpWebRequest)WebRequest.Create("urlgoeshere"); httpRequest.ClientCertificates = certificate; HttpWebResponse response = (HttpWebResponse)httpRequest.GetResponse();
So I am curious if anyone has seen this before and if they have what the difference is? And how you could get this fixed to work with the HttpClient?