web.config

来源:互联网 发布:vr资源 知乎 编辑:程序博客网 时间:2024/06/04 01:03


webconfig文件详解
 

--------------------------------------------------------------------------------
 

   
注释:
这一个示例。我们学了.
配置web.config文件.@
.并对.用户权限进行了设置

本章学习点:
<一>.
/**
*1.<forms>是验证具体的信息
*  <name>是cookies名.存储到本机客户端上
*  <loginUrl>是如果权限不够,则需要转向的页面
*  <protection>是保护的.all表示加密!
*
    <authentication mode="Forms">
         <forms name="401kApp" loginUrl="login.aspx" protection = "All"/>
      </authentication>

    <authorization>
  //
        <deny users="?" /> <!-- 允许所有用户 -->
    </authorization>
    
<二>    
    
    //删除cookies
   System.Web.Security.FormsAuthentication.SgnOut();
   
<三>
在用户少量的时候.你可以.使用如下.进行验证..
<forms name="401kApp" loginUrl="login.aspx" protection = "All">
   <credentials passwordFormat = "Clear"><!--md5的性能比较好.sha1的安全性比较好,clear是明文-->
               <user name="aa" password="aa "/>
               <user name="bb" password="aa"/>
               <user name="cc" password="cc"/>
            </credentials>
         </forms>
         
  //页面上调用方法..验证..

if(System.Web.Security.FormsAuthentication.Authenticate(this.TextBox1.Text,this.TextBox2.Text))
   {
    System.Web.Security.FormsAuthentication.SetAuthCookie(this.TextBox1.Text,false);
    Response.Redirect("successPage.aspx");

   }
   else
   {
    Response.Write("用户不合法!");
   }
   
   
<四>
 //如果为ture则.你第一次. 登录下次登录.则不需要.进入login.aspx页面.它就是..设置用户名cooices的永久性..可以跨web serveices的

   //如果为false...你下次登录时.它不会.把你的. 用户名.记住

  //以下是两种方式
   //System.Web.Security.FormsAuthentication.SetAuthCookie(this.TextBox1.Text,true);

   
   System.Web.Security.FormsAuthentication.RedirectFromLoginPage(this.TextBox1.Text,false);
   
<五>
//.调用SHA1加密..它的安全性比较好
   Response.Write(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(this.TextBox1.Text,"SHA1"));
   
<六>
//调用MD5加密
   Response.Write(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(this.TextBox1.Text,"MD5"));
《!--以上两种算法的用法.都得改相应的web.config文件-->
如下:
  <authentication mode="Forms">
         <forms name="401kApp" loginUrl="login.aspx" protection = "All">
   <credentials passwordFormat = "MD5"><!--md5的性能比较好.sha1的安全性比较好,clear是明文-->
               <user name="aa" password="4124BC0A9335C27F086F24BA207A4912 "/>
               <user name="bb" password="21AD0BD836B90D08F4CF640B4C298E7C"/>
               <user name="UserName3" password="SHA1EncryptedPassword3"/>
            </credentials>
         </forms>
      </authentication>
      
      
      
      (补充)
 <七>
 web.config是一个xml文件.用于存储应用程序的配置信息,所有的子目录配置都extends于它.
 .它的好处是.它无需.重启服务器就.就可以.生效...!dll就不会.
 
 asp.net配置文件信息
 1.web.config
 2.Machine.config
 目录在c:WinNTMicrosoft.NetFramework .1.xxxconfigMachine.config
 vs2003与vs2005的目录有所不同..
 Machine.config是针对一台服务器的.是全局的.
 web.config是设置和.其下的.子目录配置.
 如果应用程序没有.web.config...则.以Machine.config为替代!
 
 子目录配置.有两点.
1.可以配置web.config文件以外的配置信息.
2.可以重写或修改父目录中的信息.

<configuration>标记....所有的.配置信息都在.些标记内
 它分为两个:
 1.配置节处理程度声明区
 2.配置节设置区
 
 如查声明了<configSections>标记.
 则.<configuration>则.<configSections>标记一定是它的. 第一个标记.
 
 <pages>标记
 它有三个如下:
 1.appcation
 2.system.web
 3.location
<pages>标记用于特定页.(如是启用会话状态,视图状态.是否检测用户的输入等.)
它的元素如下:
buffer:[true/false]为ture则一次性把数据显示到页面上.为false则相反.,默认为ture(16个字节为一个单元)
enableviewstate:[ture/false]表是否启用viewstate控件

<appsettings>标记
表应用程序的设置.
如下操作:
例一:
<appsettings>
    <add key="conselect" value="select * from userinfo"/>
</appsettings>

例二:
<appsettings>
 <add key="dbconn" value="server=.;database=pubs;uid=sa;pwd=;"/>
</appsettings>

外部调用可以用:(如下)
System.Configuration.ConfigurationSettings.AppSettings["key值"].ToString();

为什么要这样做呢? 我们.在页面上写.和在类中写不是一样吗?
我来帮你们解释:
你把连接数据库的代码放在类中.它们会生成dll.如果我们.要.更改服务器.数据库.
那dll则需要重新编辑.来替代原来的dll..这要一来.就很不方便..
如果是 用这种方式.来设置..随你怎么改.它都不会影响.你的什么东西的.!
还是那句话.
它无需.启动.服务器.就可以生效!

<compilcation>标记
用于编辑设置
元素如下
1.debug:如果为false.则不会.记录文本文件...如果为ture.则相反..不过.它的速度会.比较慢.所以.你在给客户之前.你得.改为false;..不允许调试
2.defaultlanguage

<cusomErrors>设置
用于自定义错误
元素如下:
defaultredirect="url":可以指定 .一个自定义的错误页面.,则.无论你的应用程序发生什么错误.它都会. 转到这个.页面.
mode="on/off/remoteonly":
为on则.在本机和远程都显步自定义错误页面.
为off,则不显示详细的错误信息.
为remoteonly则.在本机显示自定义.错误页面.在远程不显示.
<error statuscode=404 redirect="url">是它的子标记.有了它则.会根据它的statuscode为条件..这个是.如果错误是404则不调用defaultredirect.
它会调用自身的redirect为定错误页面..

授权和身份验证(记得.得反.匿名.帐号的钩去了.)
身份验证
<authentication>标记
主要元素如下:
 <authentication mode="Forms"> 
 /**有四种 windows/forms/passport/none;
 用的最多的也就是forms.
 windows是机于windows身份来验证
 passport:是机于微软的身份验证
 node:是无验证,可以自己定义
 */
<forms name="name" 
       loginUrl="url"
       protection="All|None|Encryption|Validation"
       timeout="30"
       path="/" 
       requireSSL="true|false"
       slidingExpiration="true|false">
   <credentials passwordFormat="format"/>
</forms>
/**
*name:为cookies名.存储在本机
*loginurl:如果是匿名用户.则转向.些页面
*.protection:是保护的.表加密和更改
*path:应用程序发出cookies的路径.默认为"/"
*.requiressl:表是否按安全身份为登录到cooikes..ture为可以 .默认为false;
*.slidingExpiration:是否启用弹性过期时间..默认为ture
<!--md5的性能比较好.sha1的安全性比较好,clear是明文-->

如下例子:
  <authentication mode="Forms">
         <forms name="401kApp" loginUrl="login.aspx" protection = "All">
   <credentials passwordFormat = "Clear"><!--md5的性能比较好.sha1的安全性比较好,clear是明文-->
               <user name="aa" password="4124BC0A9335C27F086F24BA207A4912 "/>
               <user name="bb" password="21AD0BD836B90D08F4CF640B4C298E7C"/>
               <user name="UserName3" password="SHA1EncryptedPassword3"/>
            </credentials>
         </forms>
      </authentication>
      
授权
<authorizcation>标记
元素如下:
<allow users="">
<deny users="">
*表所有用户
?表匿名用户
注: 一定要记住<authentication>标记与<authorizcation>标记要一起使用.不然.不用

可以在页面上用以下代码.为调用:
System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(this.TextBox1.Text,"MD5") //针对于.安全问题
System.Web.Security.FormsAuthentication.SetAuthCookie(this.TextBox1.Text,this.TextBox2.Text);
//setauthcookie只写入cookies,而不转页.可能用Response.redirect()来转页..下面.这个.既写入cookies.也转页
System.Web.Security.FormsAuthentication.RedirectFromLoginPage(this.TextBox1.Text,false/ture);

相关代码如下:
例:
if(System.Web.Security.FormsAuthentication.Authenticate(this.TextBox1.Text,this.TextBox2.Text))
   {
    Response.Write(User.Identity.Name);
    System.Web.Security.FormsAuthentication.SetAuthCookie(this.TextBox1.Text,false);
    Response.Redirect("successPage.aspx");

   }
   else
   {
    Response.Write("用户不合法!");
   }

<httPRuntime>标记
作用:配置asp.net中的运行库信息.
可用于.计算机.站点.子目录.应用程序声明.
主要元素有:
maxrequestionstlength="大小",如果为4096则为1M
executiontimeout="60"..表:最大时间为60
apprequestionquexelimit:=100.表最多的用户请求数.为100个

<sessionstate>标记
用于当前.的会话状态配置
1.是否启用会话
2.会话状态保留.
它有以下元素
mode="inpro"在本地存储会话状态
cookieless=ture/false 表:如果有限制cookies,则启动session
timeout超时...默认为false

如下:
<sessionState mode="Off|InProc|StateServer|SQLServer"
              cookieless="true|false"
              timeout="number of minutes"
              stateConnectionString="tcpip=server:port"
              sqlConnectionString="sql connection string"
              stateNetworkTimeout="number of seconds"/>

 

<trace>配置跟踪信息
元素如下
<trace enabled="true|false" 为false则不启用
       localOnly="true|false" 
       pageOutput="true|false"
       requestLimit="integer" 限定跟踪数目
       traceMode="SortByTime|sortByCategory"/>
       
       默认为SortByTime;

(补充)
session在客户端使用cooikes来存储

machine.config文件中
<add verb="*" path="*.mdb" type="System.Web.HttpForbiddenHandler"/><!--新增,用来限制mdb-->
 

原创粉丝点击