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

Viewed 333354

I am getting following error when i am trying to connect Production DB from Local Environment.

I was able to connect Production DB before, but suddenly i am getting following error, any idea?

A connection was successfully established with the server, but then an error occurred during the pre-login handshake. (provider: TCP Provider, error: 0 - The handle is invalid.)

I was trying to run asp.net website in local PC, which has connection string of Production DB, following is stack trace for error I am getting in local environment.

>    at MyWebsiteDAL.clsForumQuestion.SelectAll(Int32 CurrentPageIndex, Int32 PageSize) in D:\EDrive\My WebSites\MyWebsite\MyWebsite\MyWebsiteDAL\clsForumQuestion.cs:line 821
       at CodeConnect.Default.Page_Load(Object sender, EventArgs e) in D:\EDrive\My WebSites\MyWebsite\MyWebsite\MyWebsite\Default.aspx.cs:line 100
       at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
       at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
       at System.Web.UI.Control.OnLoad(EventArgs e)
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

Any idea what might have gone wrong here?

29 Answers

In my case this error occurred with dot net core and Microsoft.Data.SqlClient. The solution was to add ;TrustServerCertificate=true to the end of the connection string.

Tried most of the above and nothing worked. Then turned off the VPN software (NordVPN) and everything was fine.

I tried many solutions in the past month, none has worked. The problem is the Production DB is within a VPN and somehow the ISP provider thinks the HTTP connection is not secure. However, I was able to connect to the same Production DB from SSMS (uses TCP).

My solution: use the mobile phone as a hotspot and use mobile data instead of my office/home Wi-Fi.

In my case, i was getting the error when i wanted to access a remote database. However, i solved it by starting SQL Server Browser service.

enter image description here

If you are connecting to an old SQL Server:

Switch from System.Data.SqlClient.SqlConnection to System.Data.OleDb.OleDbConnection

Use an OleDb connection string:

<connectionStrings>
<add name="Northwind" connectionString="Provider=SQLOLEDB.1; Data Source=MyServer; Initial Catalog=Northwind;  Persist Security Info=True; User ID=abc; Password=xyz;" providerName="System.Data.OleDb" />
</connectionStrings>

In my case, the connection from my API to the DB worked fine when running on my local machine, this error only occurred when running the .NET Core 5 API application in a docker container utilizing base image: mcr.microsoft.com/dotnet/aspnet:5.0. The problem was a TLS version inconsistency between SQL Server and one of our Dependencies. The fix was to add the following line to the API Dockerfile, downgrading security level of TLS:

RUN sed -i 's/DEFAULT@SECLEVEL=2/DEFAULT@SECLEVEL=1/g' /etc/ssl/openssl.cnf

This was taken from this article wherein the aforementioned RUN command was suggested, which fixed my problem. Direct link to the suggested fix is here.

In my case, it was a TLS protocol mismatch between a Raspberry Pi hosting a .Net Core 3.1 app requesting to a Windows Server 2012 with MSSQL. I tried many changes of TLS protocol version on the Windows Server side without any effect. I finally found this post https://github.com/dotnet/SqlClient/issues/126, indicating modify the client side, ie to update the /etc/ssl/openssl.cnf on the RPi side, and that worked great for me.

For me I had this happen on a HyperV virtual machine that was accessing my database on my local (host) machine. Basically it all works normally, but after a reboot there is some weird condition that this error starts coming up from the virtual machine applications. There is something in the network layer that is getting horribly confused. However, I have found if I ping the virtual server from the host server, it seems to fix the problem (not sure why exactly). But this might help someone.

Another thing to check is if you have your SQL Server Network Configuration set to 'Force Encryption'. If you do, you'll get this error unless you do the requisite client incantations for an encrypted connection.

#macusers

I have found new solution without closing the Visual Studio.

  1. Go to the terminal.

  2. lsof -i:<PORT_Number> i.e. 1433 in my case.

  3. Outout will be process using that port number

  4. Kill those processes using kill -9 <PID> i.e. process Id

Please refer Screenshot attached.

commands and respective outputs for killing process

In my case it was a wrong port. I connected to the on-prem DB from an Azure App Service via a Hybrid connection and the connection used one port, but the SQL Server instance used a different port. Make sure all of the ports are matching: connection string, VPN/Proxy/Hybrid connections, SQL Server allowed ports for each protocol.

sqlcmd and ping/tcpping are great tools to test your connectivity before you try changing any code in your app. You can run them both locally and in the cloud.

Related