1

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)

PaulDurant
  • 51
  • 1
  • 4
  • "Using a webservice is an unnecessarily expensive solution in this type of configuration" -- not everyone will agree with that assessment. – CommonsWare Jun 20 '23 at 10:31
  • FYI: A relevant discussion, including [if I read correctly] possible work-around for an IT situation where you could put certificates directly on each Android phone, see [SQL Server pre-login handshake failed Android](https://github.com/dotnet/SqlClient/issues/1656). Note that it starts out as a discussion on lower Android versions, but later on is at least one Android 11 comment. I don't know if Android 13 introduces any new problems. – ToolmakerSteve Jun 20 '23 at 17:57
  • Ok. You can install SQL Server, open TCP ports, set passwords, make users, install certificates on devices, waste time fixing those android problems, but the webservice is "expensive solution"? Setting up ASP.NET web API with EF is done in one day. Also there is no difference. The same C# code. You just write it in controller instead of a page. – H.A.H. Jun 21 '23 at 13:19
  • 1
    @ToolmakerSteve your comment pointed me in the right direction. The (appropriately named) DangerousTrustProvider was the solution. – PaulDurant Jun 21 '23 at 14:27

1 Answers1

1

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.

Guangyu Bai - MSFT
  • 2,555
  • 1
  • 2
  • 8