A connection was successfully established with the server, but then an error occurred during the pre-login handshake.

suresh tanukula 15 Reputation points
2023-08-10T06:08:11.2633333+00:00

I am getting below exception for database connection with MAUI android emulator. Can you please help us to resolve issue.

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)'

My connection string:

connString = "Data Source=;Database=;User Id=;Password=***;Encrypt=True;TrustServerCertificate=True";

Exception:

-		$exception	{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.<ForceAuthenticationAsync>d__146`1[[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 615
   at ESCMSMauiApp.Views.GetScanData.GetHSHData(Int64 hologramnumber) in C:\Users\stanukula\source\repos\ESCMSMauiApp\ESCMSMauiApp\Views\GetScanData.cs:line 34
   at ESCMSMauiApp.Views.HologramScan.GetHologramData() in C:\Users\stanukula\source\repos\ESCMSMauiApp\ESCMSMauiApp\Views\HologramScan.xaml.cs:line 37
   at ESCMSMauiApp.Views.HologramScan.Button_Clicked(Object sender, EventArgs e) in C:\Users\stanukula\source\repos\ESCMSMauiApp\ESCMSMauiApp\Views\HologramScan.xaml.cs:line 56
   at Microsoft.Maui.Controls.Button.Microsoft.Maui.Controls.Internals.IButtonElement.PropagateUpClicked() in D:\a\_work\1\s\src\Controls\src\Core\Button.cs:line 278
   at Microsoft.Maui.Controls.ButtonElement.ElementClicked(VisualElement visualElement, IButtonElement ButtonElementManager) in D:\a\_work\1\s\src\Controls\src\Core\ButtonElement.cs:line 85
   at Microsoft.Maui.Controls.Button.SendClicked() in D:\a\_work\1\s\src\Controls\src\Core\Button.cs:line 253
   at Microsoft.Maui.Controls.Button.Microsoft.Maui.IButton.Clicked() in D:\a\_work\1\s\src\Controls\src\Core\HandlerImpl\Button\Button.Impl.cs:line 24
   at Microsoft.Maui.Handlers.ButtonHandler.OnClick(IButton button, View v) in D:\a\_work\1\s\src\Core\src\Handlers\Button\ButtonHandler.Android.cs:line 153
   at Microsoft.Maui.Handlers.ButtonHandler.ButtonClickListener.OnClick(View v) in D:\a\_work\1\s\src\Core\src\Handlers\Button\ButtonHandler.Android.cs:line 174
   at Android.Views.View.IOnClickListenerInvoker.n_OnClick_Landroid_view_View_(IntPtr jnienv, IntPtr native__this, IntPtr native_v) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/obj/Release/net7.0/android-33/mcw/Android.Views.View.cs:line 2285
   at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPL_V(_JniMarshal_PPL_V callback, IntPtr jnienv, IntPtr klazz, IntPtr p0) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:line 121
ClientConnectionId:7e615876-689c-4f43-b813-b6b9968c9a6b
Error Number:-2146893019,State:0,Class:20}	Microsoft.Data.SqlClient.SqlException

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,292 questions
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,814 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,193 questions
{count} vote

3 answers

Sort by: Most helpful
  1. Javier Villegas 900 Reputation points MVP
    2023-08-10T09:37:55.2766667+00:00

    Hello

    have you tried to connect to this SQL Server using SSMS ? See if you get the same error message. If that´s the case then make sure that firewall in not blocking any SQL ports between client and server

    Configure the Windows Firewall to allow SQL Server access


  2. Bruce (SqlWork.com) 69,276 Reputation points
    2023-08-13T19:58:47.76+00:00

    Android and iOS do not support TrustServerCertificate. Neither O/S supports overriding certificate verification.

    If android apps are going to access Sqlserver directly rather than webapi proxy, you should buy a trusted certificate for the Sqlserver.

    if you want to use self signed certificates, then see the android procedures to load and trust a certificate.

    0 comments No comments

  3. İsmail Ekşi 0 Reputation points
    2023-11-27T11:28:13.19+00:00
     Thanks for your response but...
    
     public static string strConnect = @"Data Source = MSSQLSERVER2019;
                                          Initial Catalog=Ovit_Tecno;
                                          Persist Security Info=True;
                                          User ID = Ovit_Tecno; 
                                          Password=xxx;
                                          Connect Timeout = 30; 
                                          Encrypt=False;
                                          TrustServerCertificate=True;
                                          Application Intent = ReadWrite;
                                          MultipleActiveResultSets=false; 
                                          MultiSubnetFailover=True;
                                          Application Name=Ovit_Software;
                                          Pooling=True;";
    
    this is my connection string and I tried several like this following command by many users
    none of them worked
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.