加密与解密连接字符串部分





0/5 (0投票)
加密与解密连接字符串部分有时我们需要保护连接字符串,以防止任何人知道它。无论您的目的是什么
加密和解密 ConnectionString 节
有时我们需要保护连接字符串,以防止任何人知道它。无论您保护连接字符串的目的如何,现在有一种方法可以通过特殊代码来加密和解密连接字符串,我们将会看到...
在 Web.Config 文件中,我们找到了一个 <connectionStrings> 部分,它使我们能够添加连接字符串
<connectionStrings>
<add name="ConnectionString" connectionString="Provider=SQLNCLI10.1;Data
Source=My-pc\sqlexpress;Integrated Security=SSPI;Initial Catalog=Northwind"
providerName="System.Data.OleDb" />
</connectionStrings>
现在我们需要查看如何加密和解密连接字符串,不要忘记添加 System.Web.Configuration 命名空间
protected void Encryption(bool EncryptoValue)
{
Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
ConfigurationSection sec = config.GetSection("connectionStrings");
if (EncryptoValue == true)
{
sec.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
}
else
{
sec.SectionInformation.UnprotectSection();
}
config.Save();
}
protected void Button1_Click(object sender, EventArgs e)
{
Encryption(true);
}
protected void Button2_Click(object sender, EventArgs e)
{
Encryption(false);
}
现在您可以测试一下,希望对您有所帮助