用web.config文件控制对目录的访问(转贴)

用web.config文件控制对目录的访问 http://www.lemongtree.com/htmls/8091576b-602b-4ada-b760-e25efebe1b1c.aspx
 
文章作者: 幻想曲 来源: 柠檬树下 浏览次数:182
录入时间:2005-5-10 15:43:55  关键字:
 
 
前些天,经典论坛上有朋友问到这个问题。这里给出解答:

web.config文件在一个web应用程序下可以有多个,但是,决定认证方式的web.config文件只能有一个,且必需在网站根目录下,非根目录下的web.config文件不允许有节点。这里需要设定的是根目录下的节点和要保护的目录下的web.config文件的节点。

以柠檬树下为例子:

根-wwwroot下有一web.config文件,其内容有:

xml version="1.0" encoding="utf-8" ?>
<configuration>

  <system.web>
   <pages validateRequest="false"/>
    <compilation
         defaultLanguage="c#"
         debug="true"
    />
    <customErrors
    mode="Off"
    />

<authentication mode="Forms">
<forms name=".COOKIEDEMO" loginUrl="login.aspx" protection="All" timeout="60" path="/">

<credentials passwordFormat="SHA1">
<user name="LemongTree" password="F749D95DC90B930F80FC2AFCED6C5D205103DB8E" />
credentials>
forms>
authentication>
    <authorization>
        <allow users="*" />
    authorization>

    <trace
        enabled="false"
        requestLimit="10"
        pageOutput="false"
        traceMode="SortByTime"
  localOnly="true"
    />
    <sessionState
            mode="InProc"
            stateConnectionString="tcpip=127.0.0.1:42424"
            sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes"
            cookieless="false"
            timeout="20"
    />
    <globalization
            requestEncoding="gb2312"
            responseEncoding="gb2312"
   />
  
system.web>

configuration>

以上是根目录下web.config的设置 

再看一下受保护的admin目录下的web.config文件

<configuration>
<system.web>
<authorization>
<deny users="?" />
authorization>
system.web>
configuration>

拒绝所有匿名用户访问该目录及目录下的所有文件,在浏览器中输入该目录下任一文件的url地址都会被转到根目录下的login.aspx文件。这样就达到了验证的目的。

当然,这里我是将用户名和密码放置在web.config文件中,大家也可以用数据表中的数据加以验证

 

原文地址:https://www.cnblogs.com/stevenxiao/p/340706.html