管理你的asp.net
网站一般胡两个服务器组成,
web服务器和配置服务器.
配置文件格式
找回配置文件
使用管理API
使用管理工具
ASP.NET 2.0提供了一系列函数用以处理网
站的各种问题,包括对网站的管理
.. 配置管理API
.. 新的配置工具
.. 配置文件加密
ASP.NET的配置文件是一个标准的基于文
本的xml文件,名为web.config,可以存在
于网站中的任何目录下
在运行asp.net的时候,多个配置文件需要按照一定顺序
调用
. 1. Base configuration settings for machine.
C:/WinNT/Microsoft.NET/Framework/v2.0.xxxxx/config/m
achine.config
. 2. Root web configuration settings for machine.
C:/WinNT/Microsoft.NET/Framework/v2.0.xxxxx/config/w
eb.config
. 3. Overridden by the configuration settings for the
site (or the root application).
C:/inetpub/wwwroot/web.config
. 4. Overridden by application configuration settings.
D:/MyApplication/web.config
. 5. Overridden by subdirectory configuration settings.
D:/MyApplication/MyDir/web.config
web.config ---可以设置为拒绝复写
Web.config还可以添加自定义的元素,如下例
<configuration> <configSections> <section
name="mySection"
type="SettingsClasses.MySectionSettings" />
</configSections> <mySection .../>
</configuration>
xml文件可以当作全局变量使用.
很多ASP.NET内建的配置元素是集合元素
对于集合元素的配置有3种基本标记,分别
是<add>,<remove>,<clear>标记
. <configuration> <system.web>
<httpHandlers>
<add path="*.webservice"verb="GET, POST"type="MyHandlers.WebServiceHandler" />
<remove path="*.asmx" verb="GET,POST" /> --移除.asmx的请求,不处理该类型请求
</httpHandlers> </system.web>
</configuration>
在某些情况下,希望首级目录的配置文件不被下
级目录重载,也就是说,首级目录的配置文件不
允许被重写,则可以使用如下配置。
. <configuration> <location path="app1"
allowOverride="false"> ---拒绝该方法重写,如果后面重写会报错.
<system.web>
<sessionState enabled="false" />
</system.web> </location> <location
path="app2" allowOverride="false">
<system.web> <sessionState enabled="false" />
</system.web> </location> </configuration>
可以在程序设计中使用
CongifurationManager类来实现对配置信息
的访问
<configuration> <appSettings> <add
key="currencyService"
value="http://www.microsoft.com/services/curren
cyService.asmx" /> <add
key="creditCardValidationService"
value="http://www.microsoft.com/services/cc.as
mx" /> </appSettings> </configuration>
ConfigurationManager.AppSettings("currencySe
rvice")
Asp.net 2.0现在提供了一套对web.config进行导航、
读取、写入操作的函数。API有如下好处
. 把配置信息作为一个强类型暴露给用户,可以方便
的访问
. API提供内建的诸多语法检查机制,保证编写的特
性都正确
. 方便的在配置信息进行导航
. 可以对本地和远程的应用进行配置读写
. 用API添加的特性马上可以被管理
管理API
WebConfigurationManager.OpenMachineConfiguration()
?2. Open root web.config on the local machine.
WebConfigurationManager.OpenWebConfiguration(null)
?3. Open web.config for the application at the root of the default web
site. WebConfigurationManager.OpenWebConfiguration("/")
?4. Open web.config for a subdirectory of the application found at
/MyApp.
WebConfigurationManager.OpenWebConfiguration("/MyApp/subdir")
?5. Open web.config for an application at the root of another site.
WebConfigurationManager.OpenWebConfiguration("/", "Another Site")
?6. Open the <location> tag for a subdirectory at the application level.
WebConfigurationManager.OpenWebConfiguration("/", null, "/subdir")
?7. Open machine.config on another computer, using credentials
provided. WebConfigurationManager.OpenMachineConfiguration(null,
"remotemachine", "user", "password")
配置文件具有多极结构,对它的访问可
以使用Congiguration.GetSection方法
获得配置文件中指定列的值
web.config加密
dpapi---web.config在别的机器无法使用,无法做平行移动.
rsa---加密和解密来管理你的web.config
1 Configuration config = Configuration.GetWebConfiguration(Request.ApplicationPath);
d'Ø=@/
2 ConfigurationSection section = config.Sections["connectionStrings"];
d'Ø=@/
3 section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");;
4 config.Update ();
加密—— pef
d'Ø=@/
C:/.../aspnet_regiis.exe -pef "connectionStrings" "你的Web项目路径"
d'Ø=@/
-prov "DataProtectionConfigurationProvider"
解密 —— pdf
/.../ aspnet_regiis.exe -pdf "connectionStrings" " 你的 Web 项目路径 "
注意事项
程序读取加密字符串时,不需要编码解密
加密和解密在同一台计算机上使用
中文路径问题
javascript加密
.net中还内嵌了那些加密方法:目前有RSA和DPAPI
Web的管理工具
getsection方法不加父节点的时候默认是从configuration