Android: SQLConnection.open Android: 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)

See original GitHub issue

Description

On Windows System.Data.SQLClient (4.8.3) connection open works fine. Same code on android throws an error:

{System.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 — Any ideas? Like the sql server connection is forced to use SSL but on server side it is not (on windows works just fine).

Steps to Reproduce

  1. Created New MAUI app
  2. On button click used code from Class Library where Used System.Data.SQLClient (4.8.3)

Here is the code:

        string connectionString = @"Server=IPADRESS,PORT;Database=Transport;User Id=sa;Password=secretpass;Encrypt=False; TrustServerCertificate=true";
        string sql = "select Vozilo from VOZILA";
        string ret = "";
        SqlConnection Connection = new SqlConnection(connectionString);
        ConnectionState ps = Connection.State;
        var tbl = new DataTable();
        var cmd = new SqlDataAdapter(sql, Connection);
        cmd.SelectCommand.CommandTimeout = 0;
        try
        {
            if (ps != ConnectionState.Open)
                Connection.Open();
            cmd.Fill(tbl);
        }
        catch (DbException ex)
        {
            ret = ex.Message;
        }
        finally
        {
            cmd = default;
            if (ps == ConnectionState.Closed)
                Connection.Close();
        }
        if (tbl.Rows.Count > 0)
        {
            ret = tbl.Rows[0][0].ToString();
        }
        return ret;

Version with bug

Preview 10 (current)

Last version that worked well

Preview 10 (current)

Affected platforms

Android

Affected platform versions

Android 10

Did you find any workaround?

No

Relevant log output

{System.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.<ForceAuthenticationAsync>d__173`1[[System.Net.Security.SyncReadWriteAdapter, System.Net.Security, Version=6.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 System.Net.Security.SslStream.AuthenticateAsClient(String targetHost)
   at System.Data.SqlClient.SNI.SNITCPHandle.EnableSsl(UInt32 options)
   at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling, String accessToken)
   at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
   at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
   at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
   at System.Data.SqlClient.SqlConnection.Open()
   at ClassLibraryTest.TringSQL.GetStringFromDB() in C:\Users\Suvad\source\repos\MauiAppTest\ClassLibraryTest\TringSQL.cs:line 29
ClientConnectionId:9e5b0efd-b5e5-4d88-8ac7-7d160301b7b4}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
lulikincommented, Dec 3, 2021

I think that this error is related. HttpClientHandler ClientCertificates is null, And I cannot add to collection.

Error

0reactions
Redthcommented, Mar 28, 2022

You will likely need to also consult the Android Network Security Config Docs if you are trying to create an exception to use a non-TLS connection to a server from within your app.

Read more comments on GitHub >

github_iconTop Results From Across the Web

A connection was successfully established with the server ...
A connection was successfully established with the server, but then an error occurred during the pre-login handshake. #1479.
Read more >
A connection was successfully established with the server ...
A connection was successfully established with the server, but then an error occurred during the pre-login handshake. (provider: TCP Provider, ...
Read more >
Problem with SQLConnection class on MAUI for Android
I'm using Visual Studio 2022 for developing app in MAUI. My app shold have direct connection with Microsoft SQL server. I'm using Microsoft....
Read more >
A connection was successfully established with the server ...
A connection was successfully established with the server, but then an error occurred during the pre-login handshake.
Read more >
An error occurred during the pre-login handshake
If you receive the error message: A connection was successfully established with the server, but then an error occurred during the login process....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found