Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save UdaraAlwis/0787f74796d22c294b91be81ff162347 to your computer and use it in GitHub Desktop.
Save UdaraAlwis/0787f74796d22c294b91be81ff162347 to your computer and use it in GitHub Desktop.
Override each Native HttpClientHandler to disable the HTTPS Certificate Validation of the endpoints that we access from our App.
iOS:
// override HTTPS Certificate Verification iOS
// In AppDelegate.cs
var iosClientHandler = new NSUrlSessionHandler();
iosClientHandler.TrustOverride += (sender, trust) =>
{
return true;
};
Services.HttpClientService.HttpClientHandler = iosClientHandler;
--------------------------------------------------------------------------------------
Android:
// override HTTPS Certificate Verification Android
/// <summary>
/// Custom AndroidClientHandler for disabling
/// Self-Signed HTTPS Certificate Validation
/// Credits: https://nicksnettravels.builttoroam.com/android-certificates/
/// </summary>
public class CustomAndroidClientHandler : AndroidClientHandler
{
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
request.Version = new System.Version(2, 0);
return await base.SendAsync(request, cancellationToken);
}
protected override SSLSocketFactory ConfigureCustomSSLSocketFactory(HttpsURLConnection connection)
{
return SSLCertificateSocketFactory.GetInsecure(0, null);
}
protected override IHostnameVerifier GetSSLHostnameVerifier(HttpsURLConnection connection)
{
return new BypassHostnameVerifier();
}
}
internal class BypassHostnameVerifier : Java.Lang.Object, IHostnameVerifier
{
public bool Verify(string hostname, ISSLSession session)
{
return true;
}
}
...
...
// In MainActivity.cs
protected override void OnCreate(Bundle savedInstanceState)
{
...
var androidClientHandler = new CustomAndroidClientHandler();
Services.HttpClientService.HttpClientHandler =
androidClientHandler;
...
}
--------------------------------------------------------------------------------------
Windows (UWP):
// override HTTPS Certificate Verification Android
// In MainPage.xaml.cs
var uwpClientHandler = new WinHttpHandler()
{
ServerCertificateValidationCallback =
(message, certificate2, arg3, arg4) =>
{
return true;
}
};
Services.HttpClientService.HttpClientHandler = uwpClientHandler;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment