-
Notifications
You must be signed in to change notification settings - Fork 292
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SQL Server pre-login handshake failed Android 5,6,8.1 #1656
Comments
Same problem here on Android 11 (Api 30). On my MAUI project on VS 2022 Preview (17.3.0 Preview 2.0) I don't have the option to change TLS (Android > options). It simply don't exists. Microsoft.EntityFrameworkCore.SqlServer 6.0.6 I trying everything with the connection string... |
@VictorCanovasA |
Yes: "...an error occurred during the pre-login handshake. (provider: TCP Provider, error: 35 - An internal exception was caught)" I think it's because you are not using Microsoft.EntityFrameworkCore.SqlServer isn't it? |
I am using EF Core 6 with SQL Server. That's odd. |
Sample application posted by me is here: |
@janseris I am using SQL Server 2019. Sadly i can't share any code by now. I can't figure out what's wrong so I'll continue investigating. ¯_(ツ)_/¯ |
Could you post stats for all android versions (handshake error or not) ? That would help a lot! |
@janseris Thank you for all your help but I created an SQL Azure Database and it works perfect. In production there isn't a local SQL Server so I continue in this way to avoid losing more time. |
Hi, Microsoft.Data.SqlClient throws error 35 under couple of circumstances:
Now the question is were the users able to make the connection with previous versions of M.D.S any version before 4.0 ? If yes they probably have certificate issues. Are you able to test with the latest preview release of M.D.SqlClient. Simply add a package reference to your csproj with this version as below: <ItemGroup>
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.0.0-preview3.22168.1" />
</ItemGroup> if the issue persists try adding |
Hi. I appreciate your deep dive into the issue. In my opinion, the issue is definitely in pre-login handshake.
The server is our company's production SQL Server 2012 which we have been using for years with both old .NET 4.5.2 WinForms apps and also EF Core 5 and EF Core 6. Everything has always worked smoothly. We are accessing the server with ancient packages such as this one: https://www.codeproject.com/Articles/15666/Data-Access-Application-Block-NET-2-0-Get-Return-V The only thing which does not work is connection with Android device for Android 8.1 and below with EF Core 6.
Also, the
I will try latest preview of standalone User is not having certificate/authentication issues, I am sure about it because on Windows I connect normally and also in SQL Server management studio. Also wrong credentials would yield a different With this info, is there anything else I can do (I will try the preview version of I think that the issue is the socket layer provider in Android (which I have no control of). |
@janseris you can directly upgrade to the latest version of M.D.SqlClient by adding that to the csproj of the project and that will overwrite the previous version. |
Hi, I tried 4.1.0 SqlClient alone which is latest stable according to NuGet and also your 5.0.0 preview which doesnt show in preleases in NuGet but works: I am using .NET MAUI. Latest VS 2022 Preview (17.3.0 Preview 2) which includes latest MAUI (it is not possible to use MAUI in a different version of VS). The result in short is: SqlClient 5.0.0 Preview does not fix the problem - the problem for Android 8.1 and lower perists. What I think is a bug/issue as well are wrong SqlException Number properties on Android. The EF package which I have been using when reporting the problem was EF Core 6.0.3 (I am not sure which SqlClient version is included in that version) Certificate details:
SqlClient Preview 5.0.0 - online behavior
version latest stable 4.1.0 - online behavior:
No internet behavior SqlClient 4.1.0:
|
for my surprise, EF Core 6.0.3 uses Microsoft.Data.SqlClient 2.1.4. My server is SQL Server 2012 with latest patch (11.0.7507.2) - release date 2021-01-12. Here is a similar issue (but not stated exact versions of Android) |
It would have been nice if you told me why you asked if it worked before 4.0.0 and the reason is the following: |
Here is a better test application for this issue. |
Is found any soultion someone? |
@ExtSolTech No and there is even worse problem with SQL Server Express 2019 - follow here and you can try their suggestion what they need to test out so they can continue |
so, it works with sql server 2012 ? if so, then just install sql server 2012 ;p |
With SQL Server 2012, it doesn't work on Android 8.1 and lower but works on Android 9 and higher. With SQL Server Express 2019, it doesn't work on any version. |
what a pity, maybe in the future, someone will fixed this bugs. or create another package to connect to android. just like jtds on java, can connect to android from that library. |
That is correct. We don't use self-signed certificates in a production environment as it does require the certificate to be installed on the user's device in order to suppress the warnings or workaround security restrictions, which is why the certificate should be generated from a Trusted Certificate Authority (e.g. DigiCert etc.), so it only need to be installed on the server, which is probably why Azure Sql Servers seems to work fine. Otherwise, another alternative is to expose a Web API instead of connecting directly to the Sql Server as it's how most mobile application work with data. Regardless, |
While I cannot disagree with, in principle, anything @lcheunglci says, and I really do appreciate that the issue is being looked into, allow me to present a different point of view on the whole 'connecting to database directly from android' theme: A lot of the temptation to use .NET to deploy to mobile comes from the ability to use already known methods and frameworks to get the job done, and having the ability to do it all inside a single project. Allow us, the developers, to determine what is a security risk for a certain application in a certain surrounding, let us determine how and when will I get my connection details, where I will store them, and how important is the data, how will I setup the sql users that the app uses, etc... |
I find an unorthodox workaround , and it works.
This line of code will set encrypt option flag to "NOT_SUP" , then later this method will inform the server do not try to encrypt the data using ssl , so it prevent the real cause of the error : The remote certificate validation callback doesn't work correctly on Android and it is tracked in dotnet/runtime#45741
WARNING: If you chose this solution , it may reduce security , because the network communications between server and client is not protected by ssl. |
Thank you for tip. Also thanks for mentioning the issue with Android, that's something I've been looking for a long time. |
I haven't done further testing yet , but i guess since encryption is disabled , all the datas are transfered in plaintext |
Yes, usually regardless if encryption is enabled or not, it would always encrypt the credentials; however, by hard coding the SqlClient driver to not support encryption, I believe the credentials will be sent over in plain text which makes it a security risk to consider as I mentioned in #1840. However, from my understanding following the dotnet runtime issue dotnet/runtime#45741 mentioned, there was a PR opened on the dotnet runtime repo to address the issue and would hopefully address the self-signed certificate issue on Android. dotnet/runtime#77386 |
Workaround: If you are concerned with security this is not for you. This workaround disables server certificate verification in your app. My app is running on a closed network, so i am not that concerned. I experienced this issue: I also noticed this error among some of the exception messages: For context i tried this on Android 10 and 11 using SQL Server 2019. VisualStudio 2022 17.4.2 and MAUI with .NET 7 and Microsoft.Data.SqlClient 5.0.1 I followed the guide from noelex at StackOverflow (thanks for digging into the source code). In short:
I didnt add DangerousAndroidMessageHandlerEmitter or modify manifest file. Platforms/Android/DangerousTrustProvider.cs:
MauiProgram.cs:
You could also call the Register() function from Platforms/Android/MainActivity.cs:
Update 26 NOV 2023: I tried MAUI with .NET 8 and I am not experiencing the issue anymore. 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 |
Brilliant, I confirm this workaround also works when using |
Workaround: If you are concerned with security this is not for you. This workaround disables server certificate verification in your app. My app is running on a closed network, so i am not that concerned. Waooooooooo your are a Rock Star!!!!.. |
Brilliant workaround for Closed networks.Thanks |
Hey dude, I have to thank you so much!!! I'm working on industrial devices (like Zebra, Datalogic, Athesi and so on) that connect with an on premise SQL Server and that's a very common scenario in WMS, MES and those kinds of applications that works in the LAN. It surprised me so much that Microsoft SqlClient didn't work on Android! All my previous code (from Windows CE) would not be usable, otherwise. |
Dude, I had to log in to thank you. Was absolutely pulling my hair out trying to figure this out. One question I had though, is it safe to assume that if this works, I probably only need to add the Self-Signed cert into the trusted CA on the device? |
With the workaround you don't need the certificate. The security issue is that your app will no longer validate the identity of the server you are connecting to and therefore the certificate is not needed. When connecting to the server, it will just say: whoever you claim to be, I trust you. This is not only true for the database connection, but also for any https connection your application might make. I see three options:
I am still a bit confused about this issue. Ex.: It works when connecting from MAUI Windows app without a workaround, but not from android. It seems like the Android version insists on higher security unless it’s told otherwise. @energywave |
…qlClient#1656 WARNING IT'S NOT SAFE
Is there any solution for Release mode? |
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. |
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. I discovered one more problem on Android. Reported it but for now there is no progress on this. 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. |
…n and Android. Needed workaround from dotnet/SqlClient#1656 (comment)
Closing as resolved as per above comments. |
Issue moved from dotnet/maui#6859
Issue moved from dotnet/android#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:
Encrypt=false
in connection stringEncrypt=true
in connection stringEncrypt=false;TrustServerCertificate=true;
in connection stringThe 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+
andManaged TLS 1.0
and also for"no option"
.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+
andManaged TLS 1.0
and also for"no option"
.Similar issues:
dotnet/maui#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
The text was updated successfully, but these errors were encountered: