0

I am connecting to a Database using following connection string.

Server=tcp:mydbserverURL,1433;Initial Catalog=mydb;Persist Security Info=False;User ID=sqluser;Password=mypassword;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=True;Connection Timeout=30;

There are two ways data is being accessed in my project.

  1. Using EF Core + Automapper. - Works Well. (using a generated DBContext.cs)
  2. Using ADO.NET Repository pattern for Accessing SPs(Does not work-fails complaining login failed for user sqluser/sqladmin)

I have tried the following to resolve this issue, but with no luck.

  1. Modifying connection string to include.
  • Integrated Security=False - No Luck ;
  • Trusted_Connection=True; - No Luck either.
  1. Connecting to a different DB With the same connection string (one without VNET integration) - No Luck here as well
  2. Created a new SQL User - No Luck

Snippet for ado.net - that is failing is below.

public DataTable GetDataByStoredProc(string storedProcName, IEnumerable<KeyValuePair<string, object>> parameters)
    {
        var connectionString = _dbContext.Database.GetDbConnection().ConnectionString;
        DataTable dt = new DataTable();
        try
        {
            using (SqlConnection sqlConnection = new SqlConnection(connectionString))
            {
                sqlConnection.Open();
                using (SqlCommand sqlCommand = new SqlCommand(storedProcName, sqlConnection))
                {
                    SqlDataAdapter adapt = new SqlDataAdapter(sqlCommand);
                    adapt.SelectCommand.CommandType = CommandType.StoredProcedure;

                    if (parameters != null)
                    {
                        foreach (var parameter in parameters)
                        {
                            adapt.SelectCommand.Parameters.Add(new SqlParameter(parameter.Key, parameter.Value));
                        }
                    }

                    adapt.Fill(dt);
                }
            }
            return dt;
        }
        catch (SqlException ex)
        {
            throw;
        }
    }

It is failing on Line SQLConnection.Open with the following error message.

Login failed for user SQLUser

DL Narasimhan
  • 731
  • 9
  • 25

1 Answers1

0

The fix was as simple as setting

Persist Security Info=True;

in the Connection String.

DL Narasimhan
  • 731
  • 9
  • 25
  • 1
    don't do that! https://stackoverflow.com/questions/30419627/persist-security-info-property-true-and-persist-security-info-property-false – Mitch Wheat Oct 01 '22 at 07:19