用C#代码加密连接字符串 [英] Encrypt connection string in c# code
本文介绍了用C#代码加密连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的C#代码中有连接字符串,因此我可以使用存储过程从数据库中获取数据.
I have connection string in my c# code, so i can get data from database using a stored procedure.
连接字符串如下:
connection.ConnectionString = "Data Source=192.168.4.33;Initial Catalog=database;Persist Security Info=True;User ID=test;Password=@test312321";
我该如何加密连接,然后在我想使用它时解密?
How can i Encrypt the connection, and then when i want to use it decrypt?
推荐答案
您可以使用此:
try
{
// Open the configuration file and retrieve
// the connectionStrings section.
Configuration config = ConfigurationManager.
OpenExeConfiguration(exeConfigName);
ConnectionStringsSection section =
config.GetSection("connectionStrings")
as ConnectionStringsSection;
if (section.SectionInformation.IsProtected)
{
// Remove encryption.
section.SectionInformation.UnprotectSection();
}
else
{
// Encrypt the section.
section.SectionInformation.ProtectSection(
"DataProtectionConfigurationProvider");
}
// Save the current configuration.
config.Save();
Console.WriteLine("Protected={0}",
section.SectionInformation.IsProtected);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
或您使用企业库数据访问应用程序块来使用RSAProtectedConfigurationProvider或DPAPIProtectedConfigurationProvider进行加密. 链接: http://msdn.microsoft.com/en-us/library/89211k9b(VS.80).aspx
or you Enterprise Library Data Access Application Block to perform the encryption using RSAProtectedConfigurationProvider or DPAPIProtectedConfigurationProvider. the link: http://msdn.microsoft.com/en-us/library/89211k9b(VS.80).aspx
这篇关于用C#代码加密连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文