C# EF 加密连接数据库连接字符串

不多说,直接上代码

    public partial class Model1 : DbContext
    {
        private static string connStr = "";
        public Model1() : base("Data Source=.;Initial Catalog=GameDb;user id=sa;password=123456")//默认随便填一个标准连接字符串,如果不是标准的你的后面设置连接字符串会出错,具体原因大家有兴趣的去深究吧
        {if (connStr == "")
            {
                connStr = ConfigurationManager.ConnectionStrings["Model1"].ConnectionString;
                connStr = SymmetricMethod.Decrypto(connStr);
            }
            base.Database.Connection.ConnectionString = connStr;
        }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
        }
    }

web.config如下

 <add name="Model1" connectionString="zgtsMV7VlXojlkp2VhEQvCsN6Pq/RsadZKfvOgqRWKPtBL8/x+2yBuSiWjfkMueEcAbFVTrI094JVLn28NnyiHlAqUl8q8g4T/ybEi4gPQPVEdUfg31xF8NLgWDCAGR9Tw+7/VTm/WRpBHxrpCwUwggB35QNgqkXo+1SQ6OmpqhrNdSuPrlzh1UoOHP/p55WrVL2TNOZ5n4v6CUmODdTVw==" providerName="System.Data.SqlClient" />
  </connectionStrings>

其中 SymmetricMethod.Decrypto  解密算法,至于加解密算法,大家自行百度吧

上一篇:jsp开发模型


下一篇:kafak日志自动删除