gpt4 book ai didi

c# - 如何加密 user.settings

转载 作者:太空狗 更新时间:2023-10-29 20:17:10 28 4
gpt4 key购买 nike

我正在 Windows 8.1 上使用 C# .NET4.0 VS2010 开发 Windows 桌面应用程序。我有一系列使用 .NET 设置机制存储的设置。它们具有用户范围,因此,当在应用程序中设置时,它们将写入 Users\username\AppData\Local\companyname\App.exe_URL_randomstuff\versionno\user.config。

这些设置包括一些我需要隐藏的用户注册信息。我的研究表明我应该能够使用 RsaProtectedConfigurationProvider 加密设置,但我为此找到的所有示例都与加密 app.config 而不是 user.config (例如 http://msdn.microsoft.com/en-us/library/system.configuration.rsaprotectedconfigurationprovider.aspx )有关。

因此,我的问题是 user.config 可以加密吗?如果可以,如何加密?我注意到,当我实例化一个 System.Configuration.Configuration 对象时,我可以将 ConfigurationUserLevel 设置为 PerUserRoamingAndLocal。当我通过调试器检查对象时,它似乎引用了正确的 user.config 文件,但是当我继续实例化 ConfigurationSection 以保护它时,它返回 null。代码如下所示:

System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.PerUserRoamingAndLocal);

ConfigurationSection connStrings = config.AppSettings;

connStrings.SectionInformation.ProtectSection(provider);

我认为 config.AppSettings 可能不正确,但我不确定用什么替换它。

非常感谢任何建议。

最佳答案

现在可以使用了。我使用 ConfigurationUserLevel.PerUserRoamingAndLocal 访问我的 user.config 文件是正确的。问题出在 config.AppSettings 上。我在正确的轨道上用 config.GetSection("Progname.Properties.Settings") 替换它,但我把命名弄错了。现在的工作代码如下:

System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.PerUserRoamingAndLocal);

ConfigurationSection connStrings = config.GetSection("userSettings/Progname.Properties.Settings");

connStrings.SectionInformation.ProtectSection(provider);

“Progname”是您的程序集的名称。感谢@neoistheone 和@hatchet 的输入。

关于c# - 如何加密 user.settings,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19866570/

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