共用方式為


控制 ASP.NET 應用程式中的授權許可權

此更新介紹如何將 <location> 標籤套用至 Web.config 檔案,以設定特定檔案和資料夾的存取權。

原始產品版本: ASP.NET
原始 KB 編號: 316871

摘要

使用此逐步指南將標記套用至 <location>Web.config 檔案,以設定特定檔案和資料夾的存取權。

在 ASP.NET 應用程式中使用表單型驗證時,只有已驗證的使用者會被授與應用程式中頁面的存取權。 未經驗證的用戶會自動重新導向至 Web.config 檔案的 屬性所loginUrl指定的頁面,以便他們提交認證。 在某些情況下,您可能想要允許使用者存取應用程式中的特定頁面,而不需要驗證。

設定特定檔案和資料夾的存取權

  1. 設定表單型驗證。 如需詳細資訊,請 參閱如何使用 C# .NET 在 ASP.NET 應用程式中實作 Forms-Based 驗證

  2. 要求將應用程式中的任何頁面自動重新導向至 Logon.aspx

  3. Web.config 檔案中,輸入或貼上下列程序代碼。

    此程式代碼會授與所有使用者 存取Default1.aspx 頁面和 Subdir1 資料夾。

    <configuration>
        <system.web>
            <authentication mode="Forms" >
                <forms loginUrl="login.aspx" name=".ASPNETAUTH" protection="None" path="/" timeout="20" >
                </forms>
            </authentication>
            <!-- This section denies access to all files in this application except for those that you have not explicitly specified by using another setting. -->
            <authorization>
                <deny users="?" />
            </authorization>
        </system.web>
        <!-- This section gives the unauthenticated user access to the Default1.aspx page only. It is located in the same folder as this configuration file. -->
        <location path="default1.aspx">
            <system.web>
                <authorization>
                    <allow users ="*" />
                </authorization>
            </system.web>
        </location>
        <!-- This section gives the unauthenticated user access to all of the files that are stored in the Subdir1 folder. -->
        <location path="subdir1">
            <system.web>
                <authorization>
                    <allow users ="*" />
                </authorization>
            </system.web>
        </location>
    </configuration>
    

    用戶可以開啟 Default1.aspx 檔案或您應用程式中資料夾中儲存的 subdir1 任何其他檔案。 它們不會自動重新導向至 Logon.aspx 檔案進行驗證。

  4. 重複步驟 3 來識別您想要允許未經驗證使用者存取的任何其他頁面或資料夾。

參考資料