[英]Encrypt connection string in c# code
我的 c# 代碼中有連接字符串,所以我可以使用存儲過程從數據庫中獲取數據。
連接字符串是這樣的:
connection.ConnectionString = "Data Source=192.168.4.33;Initial Catalog=database;Persist Security Info=True;User ID=test;Password=@test312321";
我如何加密連接,然后在我想使用它時解密?
你可以使用這個:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.