问题描述
我正在创建一个常规的 Windows 应用程序,该应用程序将分发给我部门的多个用户.我需要在 App.config 文件中包含一些连接密码,而且我显然不希望最终用户只是打开记事本查看密码.
I'm creating a regular windows application that will be distributed to several users on my department. I'll need to include some connectivity passwords on the App.config file, and I obviously don't want end-users to just fire up notepad and look at the passwords.
有几篇文章指出了如何加密/解密配置部分,但您似乎必须与可部署的解决方案共享/提供一些密钥.
Several articles point on how to encrypt/decrypt configuration sections, but it appears you have to share/ship some keys with the deployable solution.
有没有更简单的方法,只对一些设置进行加密,使它们不是用户可读的,但在重新分发程序时不需要额外的步骤或文件?很棒的好处是访问配置设置在 .NET 代码中仍然是透明的.我总是可以创建一个自定义方法来对字符串进行加盐/加密,然后在我的自定义代码中解密它,但我想知道是否有更简单的方法.
Is there a simpler way, to just cipher some of the settings so that they are not user-readable, but that don't require extra steps or files when redistributing the program? Great plus would be that accessing the configuration settings is still transparent inside the .NET code. I could always just create a custom method to salt/cipher the string and in my custom code decrypt it, but I'm wondering if there's something simpler.
非常感谢有关如何执行此操作的任何答案或文章链接.谢谢
Any answers or links to articles on how to do this are greatly appreciated. Thanks
推荐答案
如果您尝试加密 App.Config/Web.Config 中的连接字符串,您可以使用 Configuration 类来实现:
If you are trying to encrypt your connection string in your App.Config/Web.Config, you can do so using the Configuration class:
Configuration config = ConfigurationManager. OpenExeConfiguration(ConfigurationUserLevel.None);
ConfigurationSection section = config.GetSection("connectionStrings");
if (section != null)
{
if (!section.IsReadOnly())
{
section.SectionInformation.ProtectSection ("RsaProtectedConfigurationProvider");
section.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Full);
}
}
有两种方法:RsaProtectedConfigurationProvider 和 DPAPIProtectedConfigurationProvider
看到这个 --> http://www.codeproject.com/KB/cs/Configuration_File.aspx 和 http://msdn.microsoft.com/en-us/library/89211k9b(VS.80).aspx.
See this --> http://www.codeproject.com/KB/cs/Configuration_File.aspx and http://msdn.microsoft.com/en-us/library/89211k9b(VS.80).aspx.
这篇关于加密将重新分发的 App.config 文件中的部分和/或设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!