【问题标题】:Specify more than one directory in Web.Config's Location Path element在 Web.Config 的 Location Path 元素中指定多个目录
【发布时间】:2011-06-04 06:31:44
【问题描述】:

在我的 ASP.NET 的 Web Config 文件中,我定义了以下位置元素:

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

  <location path="dir1">
    <system.web>
      <authorization>
        <allow users="?"/>
      </authorization>
    </system.web>
  </location>

  <location path="dir2">
    <system.web>
      <authorization>
        <allow users="?"/>
      </authorization>
    </system.web>
  </location>

上面的例子是指定所有目录都将被匿名用户锁定,除了两个目录 dir1 和 dir2。

我很好奇是否有一种我可以使用的语法允许我在一个位置元素中定义多个目录。例如,如果我们可以做这样的事情会很方便......

  <location path="dir1,dir2,etc">
    <system.web>
      <authorization>
        <allow users="?"/>
      </authorization>
    </system.web>
  </location>

【问题讨论】:

标签: asp.net path web-config location


【解决方案1】:

你不能在 path 属性中指定多个元素,但是你可以使用 configSource 属性。

例如下面的原始 web.config 文件:

<?xml version="1.0"?>
<configuration>
  <location path="form1.aspx">
    <system.web>
      <authorization>
        <allow users="*"/>
      </authorization>
    </system.web>
  </location>
  <location path="form2.aspx">
    <system.web>
      <authorization>
        <allow users="*"/>
      </authorization>
    </system.web>
  </location>
  <location path="form3.aspx">
    <system.web>
      <authorization>
        <allow users="*"/>
      </authorization>
    </system.web>
  </location>
  <location path="form4.aspx">
    <system.web>
      <authorization>
        <deny users="*"/>
      </authorization>
    </system.web>
  </location>
  <location path="form5.aspx">
    <system.web>
      <authorization>
        <deny users="*"/>
      </authorization>
    </system.web>
  </location>
  <location path="form6.aspx">
    <system.web>
      <authorization>
        <deny users="*"/>
      </authorization>
    </system.web>
  </location>
</configuration>

可以替换为以下等效的 web.config、allow.config 和 deny.config 文件:

web.config

<?xml version="1.0"?>
<configuration>
  <location path="form1.aspx">
    <system.web>
      <authorization configSource="allow.config" />
    </system.web>
  </location>
  <location path="form2.aspx">
    <system.web>
      <authorization configSource="allow.config" />
    </system.web>
  </location>
  <location path="form3.aspx">
    <system.web>
      <authorization configSource="allow.config" />
    </system.web>
  </location>
  <location path="form4.aspx">
    <system.web>
      <authorization configSource="deny.config" />
    </system.web>
  </location>
  <location path="form5.aspx">
    <system.web>
      <authorization configSource="deny.config" />
    </system.web>
  </location>
  <location path="form6.aspx">
    <system.web>
      <authorization configSource="deny.config" />
    </system.web>
  </location>
</configuration>

allow.config

<?xml version="1.0"?>
<authorization>
  <allow users="*"/>
</authorization>

拒绝.config

<?xml version="1.0"?>
<authorization>
  <deny users="*"/>
</authorization>

随着每个部分中允许/拒绝规则数量的增加,这种方法的实用性也会增加。

【讨论】:

  • 我是否能够进一步简化,并且只有在此处设置了 configSource 的位置行?所以它基本上只是一个位置列表?
  • @Ray:当我尝试这样做时,我得到了Unrecognized attribute 'configSource'
【解决方案2】:

抱歉,路径属性不允许使用“,” 所以你必须为所有路径写标签, 或者您可以在每个目录中创建 web.config。

【讨论】:

    【解决方案3】:

    可以设置特定文件夹的路径。 例如我们有一些aspx页面:

    • /data/pages/form1.aspx
    • /data/pages/form2.aspx
    • /data/pages/form3.aspx

    通过在 web.config 中创建此规则:

    <location path="data/pages">
        <system.webServer>
            <httpProtocol>
                <customHeaders>
                    <remove name="X-Frame-Options" />
                    <add name="X-Frame-Options" value="SAMEORIGIN" />
                </customHeaders>
            </httpProtocol>
        </system.webServer>
    </location>
    

    data/pages 中的所有资源都会受到影响。

    【讨论】:

      【解决方案4】:

      我遇到了类似的问题。所以采用创建单独标签的正常方式,没有其他更好的解决方案。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-05-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-27
        • 1970-01-01
        相关资源
        最近更新 更多