1

My config has Connection string with encrypted password. Code is using Entity Framework System.Data.Entity.DbContext where it holds encrypted password. How can I customize System.Data.Entity.DbContext.Database.Connection.ConnectionString to use decrypted password.

Below code DrcMaster is throwing an erro :Login failed ( as its trying to use encrypted password)

using System;
using System.Data.Entity;
using System.Configuration;

namespace DrcAuthentication.Database.User {
    public class UserContext : DbContext
    {
        public UserContext()
        {
            System.Data.SqlClient.SqlConnectionStringBuilder csb = new System.Data.SqlClient.SqlConnectionStringBuilder(ConfigurationManager.ConnectionStrings["UserContext"].ConnectionString.ToString());
            csb.Password = EncryptionUtils.Decrypt(csb.Password);
            string myCs = csb.ToString();
            Database.Connection.ConnectionString = myCs;
            //db.Database.Connection.ConnectionString = myCs;
            this.Configuration.LazyLoadingEnabled = false;
            this.Configuration.ProxyCreationEnabled = false;
        }

        public DbSet<DrcAuthentication.DrcMaster> DrcMasters { get; set; }
        public DbSet<DrcAuthentication.AuthenticatedUser> Users { get; set; }
        public DbSet<DrcAuthentication.UserRole> UserRoles { get; set; }
        //public IDbSet<SuperSecured> SuperSecured { get; set; }
    }
}
1
  • After decryption, does myCs contain a valid connection string? Commented Dec 10, 2015 at 18:36

1 Answer 1

3

Don't use Database.Connection.ConnectionString to set the connection string. The DbContext class has a constructor that takes in a connection string. You can move the the logic that obtains the connection string and decrypts it to a static method, and then construct the base DbContext class from the constructor of UserContext like this:

public class UserContext : DbContext
{
    public static string GetConnectionString()
    {
        System.Data.SqlClient.SqlConnectionStringBuilder csb = new System.Data.SqlClient.SqlConnectionStringBuilder(ConfigurationManager.ConnectionStrings["UserContext"].ConnectionString.ToString());
        csb.Password = EncryptionUtils.Decrypt(csb.Password);
        string myCs = csb.ToString();
        return myCs;
    }

    public UserContext()
        :base(GetConnectionString())
    {
        this.Configuration.LazyLoadingEnabled = false;
        this.Configuration.ProxyCreationEnabled = false;
    }



    public DbSet<DrcAuthentication.DrcMaster> DrcMasters { get; set; }
    public DbSet<DrcAuthentication.AuthenticatedUser> Users { get; set; }
    public DbSet<DrcAuthentication.UserRole> UserRoles { get; set; }
    //public IDbSet<SuperSecured> SuperSecured { get; set; }

}

However, I would recommend that instead of doing this, move the responsibility of obtaining and decrypting the connection string to another class. This leaves the UserContext class like this:

public class UserContext : DbContext
{
    public UserContext(string connection_string)
        :base(connection_string)
    {
        this.Configuration.LazyLoadingEnabled = false;
        this.Configuration.ProxyCreationEnabled = false;
    }

    public DbSet<DrcAuthentication.DrcMaster> DrcMasters { get; set; }
    public DbSet<DrcAuthentication.AuthenticatedUser> Users { get; set; }
    public DbSet<DrcAuthentication.UserRole> UserRoles { get; set; }
    //public IDbSet<SuperSecured> SuperSecured { get; set; }

}

Then, another class would inject the decrypted connection string into the UserContext class.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi @Naj. If this answer has solved your question, please consider accepting it by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.