MAUI Android unable to connect to SQL Server

660 views Asked by At

This error is seen multiple times on the internet but no satisfactory solution is given.

Connection with Microsoft.Data.SqlClient produce a SqlException (0x80131904): A connection was successfully established with the server, but then an error occurred during the pre-login handshake. (provider: TCP Provider, error: 35 - An internal exception was caught)

Head of stack is: {Microsoft.Data.SqlClient.SqlException (0x80131904): A connection was successfully established with the server, but then an error occurred during the pre-login handshake. (provider: TCP Provider, error: 35 - An internal exception was caught) ---> System.Security.Authentication.AuthenticationException: Authentication failed, see inner exception. ---> Interop+AndroidCrypto+SslException: Exception of type 'Interop+AndroidCrypto+SslException' was thrown. --- End of inner exception stack trace --- at System.Net.Security.SslStream.d__1461[[System.Net.Security.SyncReadWriteAdapter, System.Net.Security, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a]].MoveNext() at System.Net.Security.SslStream.AuthenticateAsClient(SslClientAuthenticationOptions sslClientAuthenticationOptions) at System.Net.Security.SslStream.AuthenticateAsClient(String targetHost, X509CertificateCollection clientCertificates, SslProtocols enabledSslProtocols, Boolean checkCertificateRevocation) at Microsoft.Data.SqlClient.SNI.SNITCPHandle.EnableSsl(UInt32 options) in D:\a\_work\1\s\src\Microsoft.Data.SqlClient\netcore\src\Microsoft\Data\SqlClient\SNI\SNITcpHandle.cs:line 626 at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction) in D:\a_work\1\s\src\Microsoft.Data.SqlClient\src\Microsoft\Data\SqlClient\SqlInternalConnection.cs:line 776

I have read comments that say that communication must be encrypted. So I set up a test SQL server with a domain self-signed SSL certificate. (with Force Encryption : Yes) CA certificate has been installed in the Android device.

I use this connection string:

"Server=FQDNofSqlServer; Database=xxx; User Id=sa; password=yyy;Encrypt=True;";

This connection with this string in net7.0 succeeds and is correctly encrypted. In net7.0-android the error occurs.

This kind of connection was possible in Xamarin in .net Standard 2.1.

(The Android app is an an application intended to be used in a local network and the SQL server is not publicly accessible. Using a webservice is an unnecessarily expensive solution in this type of configuration)

1

There are 1 answers

2
Guangyu Bai - MSFT On

Just as ToolmakerSteve mentioned that You can create DangerousTrustProvider.cs and placed it in the folder: Platforms/Android to disable server certificate verification on Android.

More information you can check this workround about disable server certificate verification on Android.