Finally managed to get it work :D For those who are interested, here's how I achieve it :
TL;DR: Upgrade to Visual Studio 2019 and HttpWebRequest comes to the rescue (you can find the code sample below) ;-)
Context: our project consists of a PCL and a Android project. In the team, we know we have to migrate the PCL to a .NET Standard project but it takes time, especially when you have a lot of librairies to deal with (libraries that are not updated to .NET Standard) xP
When you want to call an API, the first thing that comes to your mind is to use the HttpClient/HttpRequestHandler pair where we just have to pass our certificate in the HttpRequestHandler as follows :
httpRequestHandler.ClientCertificates.Add(new X509Certificate2(..))
Why it didn't work ? Because we're developing with Xamarin.Android which uses under the hood Mono.Droid, therefore we're meeting the unpopular NotImplementedException() ! What about WebRequestHandler ? Well the same fate :P
Hopefully the salvation came from HttpWebRequest as follows :
private Task<string> ExecuteRequest(Uri uri, X509Certificate2 certificate)
{
// Create a web request that points to our secured Backend API
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
if (certificate != null)
{
// Associate the certificates with the request
request.ClientCertificates.Add(certificate);
}
// Launch the web request
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// Output the stream to a jsonTextReader or anything else depending on your needs
using (Stream stream = response.GetResponseStream())
using (StreamReader sr = new StreamReader(stream))
using (var jsonTextReader = new JsonTextReader(sr))
{
// Do whatever you want
}
}
This code worked on my machine (Visual Studio 2019) but not on my colleague (Visual Studio 2017) : indeed the following exception was met :
System.Security.Authentication.AuthenticationException: A call to SSPI failed, see inner exception.
I also have VS2017 installed on my machine so I've tried to execute the same code with it and as strange as it sounds, I also got the error
Et voilà :) Of course, the certificate had to be "an Embedded resource"
javax.net.ssl.SSLException: Not trusted server certificate exception.I do not know which specific features did you want to acheved, If you want to connect server by SSL, it could be acheved.devblogs.microsoft.com/xamarin/…