本文介绍了在app.config C#中使用哪种方法加密我的连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的连接字符串是,



My connection String is,

<connectionString value="Data Source=HEMTECH\SA;Initial Catalog=HemWizard;User ID=sa; Password=hemtech;" />





我的加密连接字符串是(我想在上面的连接字符串下面输出)





My Encrypted connection string is ( i want below output for above connection string )

<add key="DBConnString" value="RGF0YSBTb3VyY2U9SEVNVEVDSFxTQTtJbml0aWFsIENhdGFsb2c9SGVtV2l6YXJkO1VzZXIgSUQ9c2E7IFBhc3N3b3JkPWhlbXRlY2g7"/>





我不能像上面的加密连接字符串那样帮助我如何加密我的连接字符串。



我尝试过:





I can't do like above encrypted connection string so help me how to encrypt my connection string.

What I have tried:

public static void EncryptConnectionString(bool encrypt, string fileName)
{
Configuration configuration = null;
try
{
// Open the configuration file and retrieve the connectionStrings section.
configuration = ConfigurationManager.OpenExeConfiguration(fileName);
ConnectionStringsSection configSection = configuration.GetSection("connectionStrings") as ConnectionStringsSection;
if ((!(configSection.ElementInformation.IsLocked)) && (!(configSection.SectionInformation.IsLocked)))
{
if (encrypt && !configSection.SectionInformation.IsProtected)
{
//this line will encrypt the file
configSection.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
}

if (!encrypt && configSection.SectionInformation.IsProtected)//encrypt is true so encrypt
{
//this line will decrypt the file. 
configSection.SectionInformation.UnprotectSection();
}
//re-save the configuration file section
configSection.SectionInformation.ForceSave = true;
// Save the current configuration

configuration.Save();
Process.Start("notepad.exe", configuration.FilePath);
//configFile.FilePath 
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

推荐答案


这篇关于在app.config C#中使用哪种方法加密我的连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 11:41