簡體   English   中英

在Web.Config的Location Path元素中指定多個目錄

[英]Specify more than one directory in Web.Config's Location Path element

在我的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>

您不能在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>

deny.config

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

隨着每個部分中允許/拒絕規則的數量增加,此方法的有用性也會增加。

抱歉,但path屬性不允許使用“,”因此您必須為所有路徑編寫標記,或者您可以在每個目錄中創建web.config。

可以設置特定文件夾的路徑。 例如,我們有一些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所有資源都將受到影響。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM