在非 ASP.Net 应用程序中加密连接字符串 [英] Encrypt connection string in NON ASP.Net applications

查看:29
本文介绍了在非 ASP.Net 应用程序中加密连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working with C# & WPF and, of course, an app.config file. I have not been able to find an example of encrypted connection strings, stored in the app.config. There's plenty of examples for ASP.NET and web.config but nothing solid for app.config. The only examples I have come across clearly state that the string is only "decode-able" (is that even a word?) on the same machine that it was first encrypted on. Are there any viable options for working with encrypted connection strings (or other data) in an app.config?

推荐答案

在 App.config 中加密 ConnectionStrings

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);
         }
     }
}

这篇关于在非 ASP.Net 应用程序中加密连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆