gpt4 book ai didi

c# - 在非 ASP.Net 应用程序中加密连接字符串

转载 作者:行者123 更新时间:2023-11-30 15:08:58 25 4
gpt4 key购买 nike

我正在使用 C# 和 WPF,当然还有一个 app.config 文件。我无法找到存储在 app.config 中的加密连接字符串的示例。 ASP.NET 和 web.config 有很多例子,但 app.config 没有什么可靠的例子。我遇到的唯一例子清楚地表明,该字符串在首次加密的同一台机器上只能“解码”(这甚至是一个词吗?)。在 app.config 中使用加密连接字符串(或其他数据)是否有任何可行的选项?

最佳答案

Encrypt ConnectionStrings in App.config

private void ProtectSection(String sSectionName)
{
// Open the app.config file.
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// Get the section in the file.
ConfigurationSection section = config.GetSection(sSectionName);
// If the section exists and the section is not readonly, then protect the section.
if (section != null)
{
if (!section.IsReadOnly())
{
// Protect the section.
section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
section.SectionInformation.ForceSave = true;
// Save the change.
config.Save(ConfigurationSaveMode.Modified);
}
}
}

关于c# - 在非 ASP.Net 应用程序中加密连接字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4975377/

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