0
我有一個C#Windows窗體應用程序。我們在Castle上使用nHibernate 2.1版本。我們正在將我們的應用程序安裝在安全保險庫中。因此,我們需要以加密格式將密碼存儲在hibernate.cfg.xml文件中。 C#代碼然後解密密碼。我如何設置nHibernate連接字符串密碼爲代碼中的解密字符串值?C#nHibernate以編程方式設置數據庫密碼
我有一個C#Windows窗體應用程序。我們在Castle上使用nHibernate 2.1版本。我們正在將我們的應用程序安裝在安全保險庫中。因此,我們需要以加密格式將密碼存儲在hibernate.cfg.xml文件中。 C#代碼然後解密密碼。我如何設置nHibernate連接字符串密碼爲代碼中的解密字符串值?C#nHibernate以編程方式設置數據庫密碼
您最好的選擇可能是使用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)。
@Andrew ....謝謝 – MikeTWebb 2014-11-25 20:31:22
@MikeTWebb:當然,很樂意提供幫助。 – 2014-11-25 20:31:52