Asp.net

允許對 web.config 中的單個文件夾進行匿名身份驗證?

  • April 27, 2012

所以這是場景,我有一個使用自定義身份驗證和成員資格提供程序的 Asp.Net 應用程序,但我們需要允許完全匿名訪問(即)應用程序中的特定文件夾。

在 IIS 管理器中,您可以設置文件夾的身份驗證模式,但設置保存在C:\Windows\System32\inetsrv\config\applicationHost.config文件中,如此處所述

為了使安裝更容易,如果我可以在我的 web.config 中設置它會很棒,但經過幾次嘗試我認為這可能是不可能的。

還有人知道嗎?

非常感謝

使用<location>配置標籤,並<allow users="?"/>允許匿名或<allow users="*"/>全部:

<configuration>
  <location path="Path/To/Public/Folder">
     <system.web>
        <authorization>
           <allow users="?"/>
        </authorization>
     </system.web>
  </location>
</configuration>

第一種方法是使用<location>配置標籤修改您的 web.config,並<allow users="?"/>允許匿名或<allow users="*"/>全部:

<configuration>
  <location path="Path/To/Public/Folder">
     <system.web>
        <authorization>
           <allow users="?"/>
        </authorization>
     </system.web>
  </location>
</configuration>

如果該方法不起作用,那麼您可以採用以下方法,該方法需要對 IIS applicationHost.config 進行少量修改。

首先,在 C:\Windows\System32\inetsrv\config\applicationHost.config 中將 anonymousAuthentication 部分的 overrideModeDefault 從“拒絕”更改為“允許”:

<section name="anonymousAuthentication" overrideModeDefault="Allow" />

overrideMode是 IIS 的一項安全功能。如果在 applicationHost.config 中的系統級別不允許覆蓋,則您無法在 web.config 中執行任何操作來啟用它。如果您在目標系統上沒有此級別的訪問權限,則必須與您的託管服務提供商或系統管理員進行討論。

其次,設置overrideModeDefault="Allow"之後,您可以將以下內容放入您的 web.config:

<location path="Path/To/Public/Folder">
 <system.webServer>
   <security>
     <authentication>
       <anonymousAuthentication enabled="true" />
     </authentication>
   </security>
 </system.webServer>
</location>

引用自:https://stackoverflow.com/questions/10351075