How to Allow anonymous access to SharePoint application pages in the _layouts directory

Often times, you need to add your own custom application pages (aspx) to SharePoint, which are deployed to the '_layouts' directory. These types of pages could consist of a custom login page, maintenance pages, or even complete web applications that you'll have live in SharePoint. But what if your web application has an authentication provider set up, such as Claims or Windows? In this article, I'll show you how easy it is to allow anonymous access to these pages without forcing a user to log in.

Create the project

  1. In Visual Studio 2010, create a new "Empty SharePoint Project" and name it "MyAnonymousWebPages".
    Click OK
  2. Enter your SharePoint site
  3. Select "Deploy as a farm solution"

Adding the custom Pages

Next, we need to map the 'layouts' directory to our project, so our files are deployed
there.

  1. Right click on the project name 'MyAnonymousWebPages' > Add > SharePoint "Layouts"
    Mapped Folder
  2. SharePoint will automatically add a folder under 'layouts' with the name of our
    project. Right click on this folder >Add > New Item > Application Page
  3. Name the page "HelloWorld.aspx"
  4. Your project should now look like this:

Allowing anonymous access

So far, we haven't done anything special. The steps we've taken is the basic approach
to adding custom application pages. In the following steps, we will inherit from "UnsecuredLayoutsPageBase". This is a base class for application pages that do not require the user to have any permissions.

  1. Open up HelloWorld.aspx.cs
  2. By default, the page is inherited from "LayoutsPageBase". Change the inheritance
    from "LayoutsPageBase" to "UnsecuredLayoutsPageBase".

    public partial class HelloWorld : UnsecuredLayoutsPageBase
  3. Next, we need to override a property of the UnsecuredLayoutsPageBase, to allow the
    anonymous access. Be sure to change the 'getter' to return true

    protected override bool AllowAnonymousAccess { get { return true; } }
  4. Add some content to your markup in the 'PlaceHolderMain'

Deploy the solution

  1. Right click on your project name, and select 'Deploy'
  2. Once deployment is complete, browse to your page by navigating to
    http://{yourSharePointUrl}/_layouts/MyAnonymousWebPages/HelloWorld.aspx
  3. See your custom page, like below (notice, by default, the ApplicationPage template
    in Visual Studio is designed to automatically inherit from the master page. You
    could instead replace the markup with markup from a standard aspx page template).