C#为配置文件加密的实现方法
本文实例讲述了C#为配置文件加密的实现方法,分享给大家供大家参考。具体实现方法如下:
一般来说,在web.config或app.config文件里我们经常会存储一些敏感信息,比如connectionStrings或者appSettings,比如像下面的文件。
<?xmlversion="1.0"?> <configuration> <system.web> <compilationdebug="true"targetFramework="4.0"/> </system.web> <connectionStrings> <addname="MyNwConnectionString"connectionString="Server=myServerAddress;Database=myDataBase;UserId=myUsername;Password=myPassword;"/> </connectionStrings> <appSettings> <addkey="User"value="myUsername"/> <addkey="Password"value="myPassword"/> </appSettings> </configuration>
usingSystem; usingSystem.Configuration;
namespaceWebConfigEncryptTest { publicpartialclassWebForm1:System.Web.UI.Page { protectedvoidPage_Load(objectsender,EventArgse) { stringuser=ConfigurationManager.AppSettings.Get("User"); stringpassword=ConfigurationManager.AppSettings.Get("Password"); stringconnectionString=ConfigurationManager.ConnectionStrings["MyNwConnectionString"].ConnectionString; } } }