Failing to connect to SQL Server 2008 R2 from Kubernetes using EF Core

3/12/2018

I am having trouble connecting to SQL Server 2008 R2 SP3 (Running on Windows Server Standard 2008 SP3) from a dotnet core 2.0 web API application using EF Core. I am hosting the app on an azure AKS Cluster and have VPN connectivity back to the on premises database server. Both the Windows Server and SQL Server are fully patched.

I have proved that I can connect to the database using the same connection string and standard SqlConnection.

The errors that I see are when trying EF are:

  1. An error occurred using the connection to database 'PagePulse' on server 'tcp:10.X.X.X,1433'.

Followed by:

  1. An exception occurred in the database while iterating the results of a query for context type 'X.Y.ServiceContext'. System.Data.SqlClient.SqlException (0x80131904): Login failed for user '<user id>'. at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling) 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, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource1 retry) at System.Data.SqlClient.SqlConnection.Open() at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open(Boolean errorsExpected) at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable1.Enumerator.BufferlessMoveNext(Boolean buffer) at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteImplementation[TState,TResult](Func3 operation, Func3 verifySucceeded, TState state) at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable1.Enumerator.MoveNext() at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable1 source, Boolean& found) at lambda_method(Closure , QueryContext ) at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass17_11.b__0(QueryContext qc) ClientConnectionId:b716623d-30a5-4380-bdef-bee002e34b28 Error Number:18456,State:1,Class:14`

I have ran a packet sniffer to see if this shows anymore. I can see the TCP connection negotiated successfully SYN, SYNACK, ACK and then a TDS7 packet and then the connection is closed.

Any help would be greatly appreciated.

-- B Buckland
ef-core-2.0
entity-framework-core
kubernetes
sql-server-2008-r2

1 Answer

3/13/2018

I figured this out. The password that I was using had an '@' in it, and for some reason the following character was being removed when it was passed from the environment variable in Kubernetes into the Pod.

-- B Buckland
Source: StackOverflow