Xamarin Pinning Certificate ServicePointManager Callback does not fire Xamarin android and iOS

Viewed 329

I have been looking at xamarin pinning certificate and I have tried the code below but Service callback never gets called.

I just added a button that makes an http call and I was expecting for the callback to be invoked

**using xamarin.forms.4.6.0.800 and .net standard 2.0 **

AppXaml

    protected override async void OnInitialized()
    {
        InitializeComponent();
        CertificateValidator.Initialize();

        await NavigationService.NavigateAsync("NavigationPage/MainPage");
    }

ViewModel

public class MainPageViewModel : ViewModelBase
{
    private readonly HttpClient httpClient;
    public MainPageViewModel(INavigationService navigationService)
        : base(navigationService)
    {
        this.httpClient = new HttpClient(new HttpClientHandler());
    }

    private DelegateCommand makeCallCommand;
    public DelegateCommand MakeCallCommand =>
        makeCallCommand ?? (makeCallCommand = new DelegateCommand(async () => await MakeCall()));

    async Task MakeCall()
    {
       var result= await httpClient.GetStringAsync("https://www.xamarin.com");
    }
}

CertificateValidator

public static class CertificateValidator
{

    private const string PublicKey = "whatever";


    public static void Initialize()
    {
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
        ServicePointManager.ServerCertificateValidationCallback = ValidateCertificate;
    }

    public static bool ValidateCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
    {
        Debugger.Break();
        return PublicKey == certificate?.GetPublicKeyString();
    }
}

In Android I have tried all below and none trigger a callback .

Happy to use native handlers but not sure where to start. Is there a nuget?

enter image description here

0 Answers
Related