使用Castle ActiveRecord通常都是使用配置文件进行数据库连接配置。然后采用如下方式初始化:
IConfigurationSource source = ConfigurationManager.GetSection("activerecord") as IConfigurationSource; ActiveRecordStarter.Initialize(Assembly.Load("ORM.Model"), source);
但是如果采用加密的数据库连接字符串如何进行解密呢?
通过反编译可发现source的children继承了MutableConfiguration类,MutableConfiguration类的Value并非是只读属性,因此可以通过如下方法进行实现加密连接字符串的解密。
IEncryption encrytion = new RSAEncryption(); IConfigurationSource source = ConfigurationManager.GetSection("activerecord") as IConfigurationSource; MutableConfiguration connectionValue = source.GetConfiguration(typeof(ActiveRecordBase)).Children["connection.connection_string"] as MutableConfiguration; connectionValue.Value = encrytion.Decrypt(connectionValue.Value); ActiveRecordStarter.Initialize(Assembly.Load("ORM.Model"), source);
经过测试,字符串已解密成正常的连接字符串。