From e55999cac0fbbf819b59c725d2af26968b3c01e6 Mon Sep 17 00:00:00 2001 From: David Britch Date: Fri, 4 Oct 2024 16:01:01 +0100 Subject: [PATCH] Use ServerCertificateCustomValidationCallback --- .../TodoREST/TodoREST/MauiProgram.cs | 1 - .../Services/HttpsClientHandlerService.cs | 54 ------------------- .../Services/IHttpsClientHandlerService.cs | 8 --- .../TodoREST/TodoREST/Services/RestService.cs | 23 +++++--- 4 files changed, 15 insertions(+), 71 deletions(-) delete mode 100644 8.0/WebServices/TodoREST/TodoREST/Services/HttpsClientHandlerService.cs delete mode 100644 8.0/WebServices/TodoREST/TodoREST/Services/IHttpsClientHandlerService.cs diff --git a/8.0/WebServices/TodoREST/TodoREST/MauiProgram.cs b/8.0/WebServices/TodoREST/TodoREST/MauiProgram.cs index 1fc1202a7..0773d5a31 100644 --- a/8.0/WebServices/TodoREST/TodoREST/MauiProgram.cs +++ b/8.0/WebServices/TodoREST/TodoREST/MauiProgram.cs @@ -16,7 +16,6 @@ public static MauiApp CreateMauiApp() fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold"); }); - builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); diff --git a/8.0/WebServices/TodoREST/TodoREST/Services/HttpsClientHandlerService.cs b/8.0/WebServices/TodoREST/TodoREST/Services/HttpsClientHandlerService.cs deleted file mode 100644 index 574b39507..000000000 --- a/8.0/WebServices/TodoREST/TodoREST/Services/HttpsClientHandlerService.cs +++ /dev/null @@ -1,54 +0,0 @@ -namespace TodoREST.Services -{ - public class HttpsClientHandlerService : IHttpsClientHandlerService - { - public HttpMessageHandler GetPlatformMessageHandler() - { -#if ANDROID - #if NET6_0 - var handler = new CustomAndroidMessageHandler(); - #elif NET7_0_OR_GREATER - var handler = new Xamarin.Android.Net.AndroidMessageHandler(); - #endif - handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => - { - if (cert != null && cert.Issuer.Equals("CN=localhost")) - return true; - return errors == System.Net.Security.SslPolicyErrors.None; - }; - return handler; -#elif IOS - var handler = new NSUrlSessionHandler - { - TrustOverrideForUrl = IsHttpsLocalhost - }; - return handler; -#elif WINDOWS || MACCATALYST - return null; -#else - throw new PlatformNotSupportedException("Only Android, iOS, MacCatalyst, and Windows supported."); -#endif - } -#if ANDROID && NET6_0 - internal sealed class CustomAndroidMessageHandler : Xamarin.Android.Net.AndroidMessageHandler - { - protected override Javax.Net.Ssl.IHostnameVerifier GetSSLHostnameVerifier(Javax.Net.Ssl.HttpsURLConnection connection) - => new CustomHostnameVerifier(); - - private sealed class CustomHostnameVerifier : Java.Lang.Object, Javax.Net.Ssl.IHostnameVerifier - { - public bool Verify(string hostname, Javax.Net.Ssl.ISSLSession session) - { - return Javax.Net.Ssl.HttpsURLConnection.DefaultHostnameVerifier.Verify(hostname, session) || - hostname == "10.0.2.2" && session.PeerPrincipal?.Name == "CN=localhost"; - } - } - } -#elif IOS - public bool IsHttpsLocalhost(NSUrlSessionHandler sender, string url, Security.SecTrust trust) - { - return url.StartsWith("https://localhost"); - } -#endif - } -} diff --git a/8.0/WebServices/TodoREST/TodoREST/Services/IHttpsClientHandlerService.cs b/8.0/WebServices/TodoREST/TodoREST/Services/IHttpsClientHandlerService.cs deleted file mode 100644 index 7ed8e4502..000000000 --- a/8.0/WebServices/TodoREST/TodoREST/Services/IHttpsClientHandlerService.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace TodoREST.Services -{ - public interface IHttpsClientHandlerService - { - HttpMessageHandler GetPlatformMessageHandler(); - } -} - diff --git a/8.0/WebServices/TodoREST/TodoREST/Services/RestService.cs b/8.0/WebServices/TodoREST/TodoREST/Services/RestService.cs index 416e43794..fa03a6ad5 100644 --- a/8.0/WebServices/TodoREST/TodoREST/Services/RestService.cs +++ b/8.0/WebServices/TodoREST/TodoREST/Services/RestService.cs @@ -9,19 +9,14 @@ public class RestService : IRestService { HttpClient _client; JsonSerializerOptions _serializerOptions; - IHttpsClientHandlerService _httpsClientHandlerService; public List Items { get; private set; } - public RestService(IHttpsClientHandlerService service) + public RestService() { #if DEBUG - _httpsClientHandlerService = service; - HttpMessageHandler handler = _httpsClientHandlerService.GetPlatformMessageHandler(); - if (handler != null) - _client = new HttpClient(handler); - else - _client = new HttpClient(); + HttpClientHandler insecureHandler = GetInsecureHandler(); + _client = new HttpClient(insecureHandler); #else _client = new HttpClient(); #endif @@ -32,6 +27,18 @@ public RestService(IHttpsClientHandlerService service) }; } + private HttpClientHandler GetInsecureHandler() + { + HttpClientHandler handler = new HttpClientHandler(); + handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => + { + if (cert != null && cert.Issuer.Equals("CN=localhost")) + return true; + return errors == System.Net.Security.SslPolicyErrors.None; + }; + return handler; + } + public async Task> RefreshDataAsync() { Items = new List();