Allow anonymous access to SharePoint application pages in the _layouts directory

Sample Code

public partial class ApplicationPage1 : UnsecuredLayoutsPageBase{    protected void Page_Load(object sender, EventArgs e)
    {
    }
}

 

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). 

Comments

Popular posts from this blog

IRM and the Object Model

This content database has a schema version which is not supported in this farm

Activate and Deactivate Feature through PowerShell