我有一个C#Windows窗体应用程序。我们将nHibernate 2.1版与Castle一起使用。我们正在将应用程序安装在安全的保管库中。因此,我们需要将密码以加密格式存储在hibernate.cfg.xml文件中。然后,C#代码将密码解密。如何在代码中将nHibernate connectin字符串密码设置为解密的字符串值?
您最好的选择可能是使用Configuration.GetProperty
和Configuration.SetProperty
来修改hibernate.cfg.xml文件中定义的配置:
var configuration = new Configuration()
.Configure();
const string connectionStringKey = "connection.connection_string";
string connectionString = configuration.GetProperty(connectionStringKey);
connectionString = Regex.Replace(
connectionString,
"Password=(.+);",
DecryptPasswordMatch);
configuration.SetProperty(connectionStringKey, connectionString);
[DecryptPasswordMatch
定义为:
static string DecryptPasswordMatch(Match m)
{
string password = m.Groups[1].Value;
password = /* some method that decrypts password */;
return string.Format("Password={0}", password);
}
您必须根据数据库引擎稍稍更改正则表达式(这对于SQL Server应该适用。)>