gpt4 book ai didi

c# - 从 C# 代码加密/解密连接字符串

转载 作者:行者123 更新时间:2023-11-30 18:41:23 27 4
gpt4 key购买 nike

有什么方法可以从我的 C# 代码中实现连接字符串的加密和解密。

像我们一样

aspnet_regiis -pe "connectionStrings"

aspnet_regiis -pd "connectionStrings"

谢谢

最佳答案

static public void ProtectSection()
{
// Get the current configuration file.
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

// Get the section.
UrlsSection section = (UrlsSection)config.GetSection("connectionStrings");

// Protect (encrypt)the section.
section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");

// Save the encrypted section.
section.SectionInformation.ForceSave = true;

config.Save(ConfigurationSaveMode.Full);

// Display decrypted configuration
// section. Note, the system
// uses the Rsa provider to decrypt
// the section transparently.
string sectionXml =
section.SectionInformation.GetRawXml();

Console.WriteLine("Decrypted section:");
Console.WriteLine(sectionXml);
}

关于c# - 从 C# 代码加密/解密连接字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6870039/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com