In a localhost environment how to set DatasyncClient to bypass SSL validation #842
-
During development of a MAUI app using DatasyncClient I need to hit controllers on a WebAPI on localhost. I am getting an error "java.security.cert.CertPathValidatorException: Trust anchor for certification path not found". This error seems to be well covered on stackoverflow. To test the solution I create a new HttpClient and set handler in a HtpMessageHandler an AndroidMessageHandlers ServerCertificateCustomValidationCallback = (httpRequestMessage, certificate, chain, sslPolicyErrors) => certificate?.Issuer == "CN=localhost" || sslPolicyErrors == SslPolicyErrors.None This works and I can hit the WebAPI controller and return data. I have tried to do a similar thing for DatasyncClient via a DelegatingHandler and have not been successful in avoiding the above java.security.cert.CertPathValidatorException error. This is the code I have tried. I suspect that I am not correctly setting the ServerCertificateCustomValidationCallback on Datasynclient. Any suggestions would be greatly appreciated.
|
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 6 replies
-
I am setting like this in MAUI for my iOS app and it works
|
Beta Was this translation helpful? Give feedback.
-
You need to create a new HttpClientHandler that automatically sets the ServerCertificateCustomValidationCallback to always return true. Then pass that into the DatasyncClientOptions as the last thing in the HttpPipeline. |
Beta Was this translation helpful? Give feedback.
-
Not sure if this is allowed but we use ngrok which avoids additional code to handle this scenario in the mobile app. It also gives you a local portal to inspect the requests being sent back and forth. I'm not affiliated to ngrok, I just found it really handy in this scenario. I appreciate it might not be possible in some dev environments. |
Beta Was this translation helpful? Give feedback.
-
ngrok used to work well for me but recently its giving SSL errors itself so I've started using MS tunnels. ngrok gives a NET::ERR_CERT_AUTHORITY_INVALID in browser. |
Beta Was this translation helpful? Give feedback.
You need to create a new HttpClientHandler that automatically sets the ServerCertificateCustomValidationCallback to always return true.
Then pass that into the DatasyncClientOptions as the last thing in the HttpPipeline.