Dela via


Kontrollera auktoriseringsbehörigheter i ett ASP.NET program

Den här uppdateringen beskriver hur du tillämpar taggen <location>Web.config-filen för att konfigurera åtkomst till en specifik fil och mapp.

Ursprunglig produktversion: ASP.NET
Ursprungligt KB-nummer: 316871

Sammanfattning

Använd den här stegvisa guiden för att tillämpa taggen <location>Web.config-filen för att konfigurera åtkomst till en specifik fil och mapp.

När du använder formulärbaserad autentisering i ASP.NET program beviljas endast autentiserade användare åtkomst till sidor i programmet. Oautentiserade användare omdirigeras automatiskt till den sida som anges av loginUrl attributet förWeb.config fil där de kan skicka sina autentiseringsuppgifter. I vissa fall kanske du vill tillåta användare att komma åt vissa sidor i ett program utan att kräva autentisering.

Konfigurera åtkomst till en specifik fil och mapp

  1. Konfigurera formulärbaserad autentisering. Mer information finns i Implementera Forms-Based-autentisering i ditt ASP.NET-program med hjälp av C# .NET.

  2. Begär att en sida i programmet omdirigeras till Logon.aspx automatiskt.

  3. I filenWeb.config skriver eller klistrar du in följande kod.

    Den här koden ger alla användare åtkomst till sidan Default1.aspx och mappen 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>
    

    Användare kan öppna Default1.aspx-filen eller någon annan fil som sparats i subdir1 mappen i ditt program. De omdirigeras inte automatiskt till Logon.aspx-filen för autentisering.

  4. Upprepa steg 3 för att identifiera andra sidor eller mappar som du vill tillåta åtkomst för av oautentiserade användare.

Referenser