Created
January 12, 2020 18:57
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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