How do I use Forms Authentication for only a portion of my site?

15340 Users read it.
by Marcellino Bommezijn (July 11, 2002)
Forms Authentication must be set in the web.config in the root of your account, as this is where your application exists. What if you want to turn on authentication for a subfolder or a single page, and not your entire application? You can set this in your web.config in the root of your account as well.

User Rating (60 votes)
1 review available | Read the reviews »
Author(s):Marcellino Bommezijn
User Level:Beginner
Language:VB.NET
Platform:Web

To set the Forms Authentication parameters, the following must be in your web.config (for example):

  <system.web>
    <!-- enable Forms authentication -->
    <authentication mode="Forms">
      <forms name="MyAuth" loginUrl="loginpage/login.aspx" protection="All" path="/" />
    </authentication>
  </system.web>


Now you must define what you want to be protected. If you want to enable it for the entire site, then place the following within the <system.web> tag as well:

    <authorization>
      <deny users="?" />
    </authorization>


However, if you want to enable it for a particular subfolder or file, then you would use <location> tags instead. The following will turn on authentication for location "store/Checkout.aspx":

  <location path="store/Checkout.aspx">
    <system.web>
      <authorization>
        <deny users="?" />
      </authorization>
    </system.web>
  </location>


You can have as many <location> tags as you want. For more information on the <location> settings, please see the MSDN Library on Microsoft's site.

Source: MaximumASP Knowledgebase

Marcellino Bommezijn

Marcellino BommezijnMarcellino Bommezijn is one of the managers at UDzone.com. He is a contributor on the tutorials section.

In daily live he is a E-Business Coordinator at Geveke Motoren bv (Dutch Caterpillar Engine Dealer) and runs his own webdesign and hosting company.

(Photo: me and my son Nicky)

See All Postings From Marcellino Bommezijn >>

User Reviews

Total of 1 review
Good primer.
Written by Afendi Hamat on August 28, 2002

How about protecting individual elements on a page? For example, say certain links are shown only if you're logged in? I used to do this using cookies/session in classic ASP, just wondering if things are changed or the same in ASP.NET?

Follow us on twitter Subscribe to our RSS feed
Activate your free membership today | Login | Currency