EntLib 自动数据库连接字符串加密

 const string provider = "RsaProtectedConfigurationProvider";
                Configuration config = null;

                config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

                // 加密连接字符串

                ConfigurationSection section = config.ConnectionStrings;

                if ((section.SectionInformation.IsProtected == false) &&

                    (section.ElementInformation.IsLocked == false))
                {
                    section.SectionInformation.ProtectSection(provider);

                    section.SectionInformation.ForceSave = true;

                    config.Save(ConfigurationSaveMode.Full);

                }

这样控制台所在目录或者IIS发布目录下连接将是加密字符串.

如果报错,说明机器上没有RSA容器,可通过

aspnet_regiis -pc "MyKeys" -exp

aspnet_regiis -pa "MyKeys" "NT AUTHORITY\NETWORK SERVICE"

 

创建,具体参见:http://msdn.microsoft.com/en-us/library/2w117ede.aspx 

参考:http://www.cnblogs.com/Terrylee/archive/2006/06/05/Enterprise_Library_Encryption_ConnectionStrings.html

posted @ 2015-01-16 16:49  tyb1222  阅读(524)  评论(0编辑  收藏  举报