對(duì)Web.config配置文件的常見操作對(duì)于配置文件的常見操作包括: l 讀取 l 修改 l 將web.config中的配置節(jié)放在單獨(dú)的文件中 l 對(duì)某一節(jié)進(jìn)行加密 l 添加定制的節(jié) 操作web配置文件(包括machine.config和web.config等)的命名空間是:System.Web.Configuration。主要應(yīng)用的類是:WebConfigurationManager。下面看看WebConfigurationManager類的成員。(可以利用MSDN來查看,我下面是利用Lutz Roeder的Reflector)
圖 1 WebConfigurationManager類成員 3.2.1 讀取 圖 2 WebConfigurationManager類中用于讀取的屬性和方法 3.2.1.1 讀取appSettings節(jié)和connectionStrings節(jié) 演示操作appSettings 節(jié)的代碼: using System.Web.Configuration; .... string message; message = WebConfigurationManager.AppSettings["message"]; ...
using System.Web.Configuration; .... string connectionString = WebConfigurationManager.ConnectionStrings["pubs"].ConnectionString; ...
圖 3 繼承于ConfigurationSection類的各section類 從中我們查找到compilation節(jié)對(duì)應(yīng)于System.Web.Configuration. CompilationSection 類,authentication節(jié)對(duì)應(yīng)于System.Web.Configuration.AuthenticationSection類,identity節(jié)對(duì)應(yīng)于System.Web.Configuration.IdentitySection類,等等。 例子1: protected void readImpersonationButton_Click(object sender, EventArgs e) { IdentitySection section; section = WebConfigurationManager.GetSection("system.web/identity") as IdentitySection; if (section != null) { WriteMessage("Impersonate = " + section.Impersonate); } } private void WriteMessage(string message) { //在頁面上添加一個(gè)名為messagePlaceHolder的PlaceHolder控件 HtmlGenericControl generic = new HtmlGenericControl(); generic.InnerHtml = message; messagePlaceHolder.Controls.Add(generic); }
例子2:(MSDN上的例子)此示例演示一個(gè)可從 Web 應(yīng)用程序或控制臺(tái)應(yīng)用程序訪問的節(jié)。 // Show the use of GetSection(string). // It gets the connectiobStrings section. // If called from within a client application, // the GetSection(string) gets the default connectionStrings // section from the machine.config. // If called from within a Web aplication it gets the // section from the configuration file located at the // application current level. static void GetSection1() { // Get the connectionStrings section. ConnectionStringsSection connectionStringsSection = WebConfigurationManager.GetSection("connectionStrings") as ConnectionStringsSection; // Get the connectionStrings key,value pairs collection. ConnectionStringSettingsCollection connectionStrings = connectionStringsSection.ConnectionStrings;
// Get the collection enumerator. IEnumerator connectionStringsEnum = connectionStrings.GetEnumerator(); // Loop through the collection and // display the connectionStrings key, value pairs. int i = 0; Console.WriteLine("[Display the connectionStrings]"); while (connectionStringsEnum.MoveNext()) { string name = connectionStrings[i].Name; Console.WriteLine("Name: {0} Value: {1}", name, connectionStrings[name]); i += 1; } Console.WriteLine(); }
3.2.2 修改 例子:把system.web項(xiàng)下的compilation修改(false->true;或true->false)。 protected void toggleDebugButton_Click(object sender, EventArgs e) { Configuration config; config = WebConfigurationManager.OpenWebConfiguration("~"); CompilationSection compilation; compilation = config.GetSection("system.web/compilation") as CompilationSection; if (compilation != null) { compilation.Debug = !compilation.Debug; config.Save(); WriteMessage("Debug setting is now: " + compilation.Debug); } } 修改的注意點(diǎn): 1. 必須對(duì)該文件有修改的權(quán)限,通常NETWORK SERVICE和ASPNET賬戶對(duì)文件和目錄沒有修改權(quán)限。 2. ASP.NET在運(yùn)行時(shí),Runtime時(shí)刻注視著web.config文件的變化,一旦該文件修改了,整個(gè)應(yīng)用程序就創(chuàng)建一個(gè)新的實(shí)例,并重新加載。如果頻繁的修改web.config文件,對(duì)程序性能影響比較大。 3. 如果需要頻繁修改配置,則應(yīng)把該配置節(jié)放到單獨(dú)的文件中。 3.2.3 將web.config中的配置節(jié)放在單獨(dú)的文件中 例子: web.config文件 <?xml version="1.0"?> <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0"> <appSettings configSource="appSettings.config"/> <connectionStrings configSource="connections.config"/> <system.web> <compilation debug="true" /> <authentication mode="Windows"/> <identity impersonate="true"/> </system.web> </configuration>
<appSettings> <add key="message" value="Hello World!"/> </appSettings>
<connectionStrings> <add name="AdventureWorks" connectionString="..."/> <add name="pubs" connectionString="..."/> </connectionStrings>
1. 可以根據(jù)不同的環(huán)境而設(shè)置不同的配置,譬如開發(fā)環(huán)境、測試環(huán)境、正式環(huán)境配置不同的數(shù)據(jù)庫連接。 2. 權(quán)限管理。例如,可以鎖住web.config不讓某些人修改,而只讓其用于修改appSettings.config文件的權(quán)限。 3. 利用外部配置文件,還可以控制當(dāng)其修改后應(yīng)用程序是否重啟。一般,當(dāng)我們修改了web.config文件時(shí),應(yīng)用程序就產(chǎn)生一個(gè)新的實(shí)例并重啟。而如果是單獨(dú)的配置文件,則可以控制重啟與否。 配置方法:在machine.config中配置。Machine.config文件一般在C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG目錄下。打開machine.config文件,截圖如下:
圖 4 machine.config文件內(nèi)容截圖 從圖4我們可以看到在有的元素中有restartOnExternalChanges屬性。如果設(shè)為false,則外部文件修改后不重啟,如果設(shè)為true,則重啟。 3.2.4 對(duì)某一節(jié)進(jìn)行加密 在配置文件中,有些信息是不希望別人看到的。例如在<connectionStrings>節(jié)中可能包含連接數(shù)據(jù)庫的用戶名和密碼。<identity>節(jié)中也可能包含用戶名和密碼。 注意:有些節(jié)可能含有密碼,但不能加密,例如<processModel>節(jié),為了安全,你可以應(yīng)用Aspnet_setreg.exe工具來保存密碼。
protected void toggleEncryptionButton_Click(object sender, EventArgs e) { Configuration config; config = WebConfigurationManager.OpenWebConfiguration("~"); ConnectionStringsSection section; section = config.GetSection("connectionStrings") as ConnectionStringsSection; if (section.SectionInformation.IsProtected) { section.SectionInformation.UnprotectSection(); } else { section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider"); } config.Save(); WriteMessage("connections protected = " + section.SectionInformation.IsProtected); }
下面是相關(guān)文件的變化。 加密前的connectionStrings.config文件內(nèi)容如下:
圖 5 加密前的connectionStrings.config文件內(nèi)容 加密后connectionStrings.config文件內(nèi)容如下:
圖 6 加密后connectionStrings.config文件內(nèi)容 對(duì)于結(jié)果的改變,我們可以對(duì)比一下,自己了解其變化。 但在上面的代碼片斷中,我們還有一個(gè)地方需要了解:加密提供者。目前有兩種加密提供者:DataProtectionConfigurationProvider 和 RSAProtectedConfigurationProvider。其中DataProtectionConfigurationProvider是利用Windows Data Protection API(DPAPI)提供與機(jī)器相關(guān)的加密解密。也就是說我們?cè)谀囊慌_(tái)機(jī)器上加密就只能在那一臺(tái)機(jī)器上解密。如果需要在其它機(jī)器上解密則只能使用DataProtectionConfigurationProvider。 3.2.5添加定制的配置節(jié) <configuration> <configSections> <section name="Logging" type="System.Configuration.NameValueSectionHandler" /> </configSections> <Logging> <add key="File Name" value="MyApp.log" /> <add key="MessageColumns" value="5" /> <add key="MaxFileSize" value="40000000000000" /> </Logging> </configuration> |
|
|