dotnet / SqlClient

Microsoft.Data.SqlClient provides database connectivity to SQL Server for .NET applications.
MIT License
845 stars 281 forks source link

SQL Server pre-login handshake failed Android 5,6,8.1 #1656

Closed jonathanpeppers closed 3 months ago

jonathanpeppers commented 2 years ago

Issue moved from dotnet/maui#6859 Issue moved from https://github.com/xamarin/xamarin-android/issues/6990


From @janseris on Thursday, May 5, 2022 12:39:34 PM

Description

SQL Server - the connection was established but pre-login handshake failed when calling database via Entity Framework

**Microsoft.Data.SqlClient.SqlException:** '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)'

What does not help:

The same works without any issue on Android 9 and up (both emulator and real device). Tested: works on Android 9 for all TLS settings: Native TLS 1.2+ and Managed TLS 1.0 and also for "no option".

image

The issue is in Debug (and thus probably also in Release) configuration.

Output for Android 5 (API 21): Microsoft.Data.SqlClient.SqlException: '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)'

Output for Android 8, for all TLS configuration options: Native TLS 1.2+ and Managed TLS 1.0 and also for "no option".

[System.err] java.lang.IllegalStateException: Handshake has already been started
[System.err]    at com.android.org.conscrypt.OpenSSLEngineImpl.beginHandshakeInternal(OpenSSLEngineImpl.java:335)
[System.err]    at com.android.org.conscrypt.OpenSSLEngineImpl.beginHandshake(OpenSSLEngineImpl.java:325)
[System.err]    at crc640ec207abc449b2ca.ShellSectionRenderer.n_onCreateView(Native Method)
[System.err]    at crc640ec207abc449b2ca.ShellSectionRenderer.onCreateView(ShellSectionRenderer.java:42)
[System.err]    at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2995)
[System.err]    at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:523)
[System.err]    at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:261)
[System.err]    at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1840)
[System.err]    at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1764)
[System.err]    at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1701)
[System.err]    at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2849)
[System.err]    at androidx.fragment.app.FragmentManager.dispatchViewCreated(FragmentManager.java:2777)
[System.err]    at androidx.fragment.app.Fragment.performViewCreated(Fragment.java:3020)
[System.err]    at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:551)
[System.err]    at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:261)
[System.err]    at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1840)
[System.err]    at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1764)
[System.err]    at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1701)
[System.err]    at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2849)
[System.err]    at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:2784)
[System.err]    at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:262)
[System.err]    at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:478)
[System.err]    at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:246)
[System.err]    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1333)
[System.err]    at android.app.Activity.performStart(Activity.java:6992)
[System.err]    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2780)
[System.err]    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
[System.err]    at android.app.ActivityThread.-wrap11(Unknown Source:0)
[System.err]    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
[System.err]    at android.os.Handler.dispatchMessage(Handler.java:105)
[System.err]    at android.os.Looper.loop(Looper.java:164)
[System.err]    at android.app.ActivityThread.main(ActivityThread.java:6541)
[System.err]    at java.lang.reflect.Method.invoke(Native Method)
[System.err]    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
[System.err]    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
**Microsoft.Data.SqlClient.SqlException:** '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)'

Similar issues: https://github.com/dotnet/maui/issues/3522

Steps to Reproduce

call database in a MAUI app with Android 8.1 or lower

Version with bug

Release Candidate 2 (current)

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android 8.1 and below

Did you find any workaround?

no

Relevant log output

No response

genifycom commented 1 year ago

devmartin Could not get this to work with Android API 33 (emulator or physical device).

DangerousTrustProvider register is being called.

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)

This is to an AWS RDS instance. SSMS connects fine with encrypt true or false.

thevirtualdj commented 12 months ago

We cannot do this on user's phone. You do not have to install cert on android (it is embedded in the app itself). image

devmartin commented 11 months ago

I tried MAUI with .NET 8 and I am not experiencing the original issue anymore "SQL Server pre-login handshake failed". Now I only have "TrustServerCertificate=True" in my connection string.

For context I tested this using android 9, 10, 11, 12 and 13 using SQL Server 2019 + 2022, Microsoft.Data.SqlClient 5.1.2, VisualStudio 2022 17.8.1

thevirtualdj commented 10 months ago

I tried MAUI with .NET 8 and I am not experiencing the original issue anymore "SQL Server pre-login handshake failed". Now I only have "TrustServerCertificate=True" in my connection string.

For context I tested this using android 9, 10, 11, 12 and 13 using SQL Server 2019 + 2022, Microsoft.Data.SqlClient 5.1.2, VisualStudio 2022 17.8.1

Thank you for the info. It seems that in Microsoft.Data.SqlClient 5.1.2 it is solved.

I discovered one more problem on Android. Reported it but for now there is no progress on this. On ANDROID first connection open is 10 times slower than on windows. https://github.com/dotnet/SqlClient/issues/2193

Hope someone will take this as a priority because this is still not usable if connection "dies" and needs reopen, or we need a new connection object it will be slow on first open.

cheenamalhotra commented 3 months ago

Closing as resolved as per above comments.