加密C#中web.config文件中的连接字符串

我已将web.config文件中的数据库名称,用户名和密码写为连接字符串。

我想加密这些数据。 我该怎么做?

    

您只需使用apnet_regiis工具即可

 C:\WINDOWS\Microsoft.Net\Framework(64)\(.Net version)\aspnet_regiis -pe "connectionStrings" 

对于特定应用程序,您可以使用app参数-app 应用程序名称 ,对于特定站点,您还可以使用站点参数“-site site id ”。

有关更多详细信息,请参阅http://msdn.microsoft.com/en-us/library/dtkwfdky.aspx 。

请注意,这仅适用于Web应用程序,不适用于Windows应用程序。

另请注意,您必须使用提升的权限(“以管理员身份运行”)从命令提示符运行它。

我是一个特定的应用程序,我在启动时调用以下例程:

 Private Sub CheckConfigFile() Dim config As System.Configuration.Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None) Dim sec As ConfigurationSection = config.AppSettings If sec IsNot Nothing Then If sec.SectionInformation.IsProtected = False Then Debug.Write("Encrypting the application settings...") sec.SectionInformation.ProtectSection(String.Empty) sec.SectionInformation.ForceSave = True config.Save(ConfigurationSaveMode.Full) Debug.WriteLine("done!") End If End If End Sub