There are times, where we need to create an application page to be accessed anonymously by the user, instead of the user signing-in and accessing the page. Here is the code-snippet to create an unsecure application page for sharepoint.
public partial class UnsecureApplicationPage : Microsoft.SharePoint.WebControls.UnsecuredLayoutsPageBase
// inherit the page from Microsoft.SharePoint.WebControls.UnsecuredLayoutsPageBase instead of Microsoft.SharePoint.WebControls.LayoutsPageBase
{
protected void Page_Load(object sender, EventArgs e)
{
}
//override the allow anonymous property to true
protected override bool AllowAnonymousAccess
{
get { return true; }
}
protected override bool AllowNullWeb { get { return true; } }
protected override void OnPreInit(EventArgs e)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
base.OnPreInit(e);
Microsoft.SharePoint.SPWeb _Web = SPControl.GetContextWeb(Context);
this.MasterPageFile = _Web.MasterUrl;
});
}
}