需要为System.Data.Entity.DbContext自定义解密密码

本文关键字:DbContext 自定义 解密 密码 Entity Data System | 更新日期: 2023-09-27 18:26:29

我的配置有带加密密码的连接字符串。代码使用实体框架System.Data.Entity.DbContext,其中包含加密密码。如何自定义System.Data.Entity.DbContext.Database.Connection.ConnectionString以使用解密的密码。

下面的代码DrcMaster抛出了一个错误:登录失败(因为它试图使用加密密码)

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; }
    }
}

需要为System.Data.Entity.DbContext自定义解密密码

不要使用Database.Connection.ConnectionString来设置连接字符串。DbContext类有一个接受连接字符串的构造函数。您可以将获得连接字符串并对其进行解密的逻辑移动到一个静态方法,然后从UserContext的构造函数中构造基本DbContext类,如下所示:

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; }
}

但是,我建议不要这样做,而是将获取和解密连接字符串的责任转移到另一个类。这使得UserContext类如下:

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; }
}

然后,另一个类将解密的连接字符串注入UserContext类。