-
-
Save Eilon/49e3c5216abfa3eba81e453d45cba2d4 to your computer and use it in GitHub Desktop.
Dev helper for establishing SSL connections to localhost on various platforms
This file contains 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
using System.Net.Security; | |
public class DevHttpsConnectionHelper | |
{ | |
public DevHttpsConnectionHelper(int sslPort) | |
{ | |
SslPort = sslPort; | |
DevServerRootUrl = FormattableString.Invariant($"https://{DevServerName}:{SslPort}"); | |
LazyHttpClient = new Lazy<HttpClient>(() => new HttpClient(GetPlatformMessageHandler())); | |
} | |
public int SslPort { get; } | |
public string DevServerName => | |
#if WINDOWS | |
"localhost"; | |
#elif ANDROID | |
"10.0.2.2"; | |
#else | |
throw new PlatformNotSupportedException("Only Windows and Android currently supported."); | |
#endif | |
public string DevServerRootUrl { get; } | |
private Lazy<HttpClient> LazyHttpClient; | |
public HttpClient HttpClient => LazyHttpClient.Value; | |
public HttpMessageHandler? GetPlatformMessageHandler() | |
{ | |
#if WINDOWS | |
return null; | |
#elif ANDROID | |
var handler = new CustomAndroidMessageHandler(); | |
handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => | |
{ | |
if (cert != null && cert.Issuer.Equals("CN=localhost")) | |
return true; | |
return errors == SslPolicyErrors.None; | |
}; | |
return handler; | |
#else | |
throw new PlatformNotSupportedException("Only Windows and Android currently supported."); | |
#endif | |
} | |
#if ANDROID | |
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"; | |
} | |
} | |
} | |
#endif | |
} |
@simonrozsival ah I'll have to try this again and see what works!
This works for me!
Thank you so much for sharing this!
Thanks!!!you are the best!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
FYI since xamarin/xamarin-android#7246 was merged, the
CustomHostnameVerifier
hack won't be necessary in .NET 7 on Android.