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
- In Visual Studio 2010, create a new "Empty SharePoint Project" and name it "MyAnonymousWebPages".
Click OK - Enter your SharePoint site
- 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.
- Right click on the project name 'MyAnonymousWebPages' > Add > SharePoint "Layouts"
Mapped Folder - SharePoint will automatically add a folder under 'layouts' with the name of our
project. Right click on this folder >Add > New Item > Application Page - Name the page "HelloWorld.aspx"
- 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.
- Open up HelloWorld.aspx.cs
- By default, the page is inherited from "LayoutsPageBase". Change the inheritance
from "LayoutsPageBase" to "UnsecuredLayoutsPageBase".public partial class HelloWorld : UnsecuredLayoutsPageBase - Next, we need to override a property of the UnsecuredLayoutsPageBase, to allow the
anonymous access. Be sure to change the 'getter' to return trueprotected override bool AllowAnonymousAccess { get { return true; } } - Add some content to your markup in the 'PlaceHolderMain'
Deploy the solution
- Right click on your project name, and select 'Deploy'
- Once deployment is complete, browse to your page by navigating to http://{yourSharePointUrl}/_layouts/MyAnonymousWebPages/HelloWorld.aspx
- 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).