控制 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 以标识要允许未经身份验证的用户访问的任何其他页面或文件夹。

References