1

I'm developing an app using Xamarin Forms (4.5.0.356).

I have to show an image which come from an HTTPS links using the default bind:

<Image Source="{Binding PhotoURL}" /> 

At runtime I have the error:

javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
    at com.android.org.conscrypt.ConscryptFileDescriptorSocket.startHandshake(ConscryptFileDescriptorSocket.java:219)
    at com.android.okhttp.internal.io.RealConnection.connectTls(RealConnection.java:192)
    at com.android.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:149)
    at com.android.okhttp.internal.io.RealConnection.connect(RealConnection.java:112)
    at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:184)
    at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:126)
    at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:95)
    at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:281)
    at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:224)
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:461)
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:127)
    at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.connect(DelegatingHttpsURLConnection.java:89)
    at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.connect(Unknown Source:0)

I know the error is caused by a self signed certificate, and I usually manage this type of error with a HttpClientHandler like the following:

var handler = new HttpClientHandler()
            {
                ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) =>
                {
                    //bypass
                    return true;
                },
            };
_httpClient = new HttpClient(handler) { BaseAddress = new Uri(Constants.HTTP_BASE_ADDRESS) };

But how can I do the Xamarin.Android.Net.AndroidClientHandler manages self-signed certificates when the bind mechanism assigns an https source to an image control?

Thanks

gianmere
  • 11
  • 2
  • In mainactivity please add this line ServicePointManager.ServerCertificateValidationCallback += (o, cert, chain, errors) => true; – Anand Apr 05 '20 at 17:11
  • and change like this var httpClient = new HttpClient(new System.Net.Http.HttpClientHandler()); – Anand Apr 05 '20 at 17:11
  • My question is not about the use of HttpClient, which for me work as I wrote. It's about how to make working: if "site" uses a self-signed certificate. – gianmere Apr 06 '20 at 16:52
  • You could check the link below. https://stackoverflow.com/questions/41160918/xamarin-forms-image-source-with-ssl – Wendy Zang - MSFT Apr 14 '20 at 07:29

0 Answers0