Allow anonymous access to custom application pages
-
22-12-2020 - |
Pregunta
I have created SharePoint Custom Application page.
I'm trying to make that application page anonymous.
So it can be viewed by any user.
For that I have did Below Workaround
- I have make web application anonymous access SharePoint 2013.
- Used unsecuredlayoutspagebase to allow anonymous access
- I have used RunWithElevatedPrivileges in code behind.
- Overriden AllowAnonymousAccess to always return true
Still it will a ask for credentials.
Please Check My Code
using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web;
using System.IO;
namespace Icalendar.Layouts.Icalendar
{
public partial class Icalendarfeed : UnsecuredLayoutsPageBase
{
protected void Page_Load(object sender, EventArgs e)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(SPContext.Current.Web.Url))
{
using (SPWeb web = site.OpenWeb())
{
SPList calendarList = web.Lists["Events"];
SPQuery query = new SPQuery();
query.ExpandRecurrence = true;
query.Query = "<Where>" +
"<DateRangesOverlap><FieldRef Name=\"EventDate\" />" +
"<FieldRef Name=\"EndDate\" />" +
"<FieldRef Name=\"RecurrenceID\"/>" +
"<Value Type=\"DateTime\"><Month /></Value></DateRangesOverlap>" +
"</Where>";
query.CalendarDate = new DateTime(DateTime.Now.Year,DateTime.Now.Month, 1);
SPListItemCollection calendarItems = calendarList.GetItems(query);
//string now = DateTime.Now.ToUniversalTime().ToString();
string now = Microsoft.SharePoint.Utilities.SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.Now); ;
System.Text.StringBuilder sw = new System.Text.StringBuilder();
sw.AppendLine("BEGIN:VCALENDAR");
sw.AppendLine("PRODID:-//Microsoft Corporation//SharePoint MIMEDIR//EN");
sw.AppendLine("VERSION:2.0");
sw.AppendLine("METHOD:PUBLISH");
foreach (SPListItem item in calendarItems)
{
// Console.WriteLine(item["Title"] + ": starts " + item["EventDate"].ToString() + " and ends " + item["EndDate"].ToString());
sw.AppendLine("BEGIN:VEVENT");
sw.AppendLine("UID;TYPE=SharePoint:" + item.ID);
sw.AppendLine(string.Format("DTSTART:{0:yyyyMMddTHHmmss}", Convert.ToDateTime(item["EventDate"])) + "Z");
sw.AppendLine(string.Format("DTEND:{0:yyyyMMddTHHmmss}", Convert.ToDateTime(item["EndDate"])) + "Z");
sw.AppendLine("LOCATION:" + Convert.ToString(item["Location"]));
sw.AppendLine("TRANSP:OPAQUE");
sw.AppendLine("SEQUENCE:0");
sw.AppendLine("SUMMARY;LANGUAGE=en-us:" + Convert.ToString(item["Title"]));
sw.AppendLine("DTSTAMP:" + string.Format("{0:yyyyMMddTHHmmss}", Convert.ToDateTime(now)) + "Z");
sw.AppendLine("DESCRIPTION: " + item["Description"]);
sw.AppendLine("CLASS:PUBLIC");
// sw.AppendLine(string.Format("CREATED:{0:yyyyMMddTHHmmss}", DateTime.UtcNow));
sw.AppendLine("END:VEVENT");
}
sw.AppendLine("END:VCALENDAR");
string FileName = "Event.ics";
Response.Clear();
Response.ContentType = "text/calendar";
Response.AddHeader("content-disposition", "inline; filename=" + FileName);
Response.Write(sw.ToString());
Response.End();
}
}
});
}
}
}
Solución
Original post :
Anonymous Access: SharePoint 2013 Custom Application Page
Try the below steps:
Step 1: Open the Central Admin and go to Manage Web Application (under Application Management) .
Step 2: Choose the Web Application you want anonymous access for and click on Authentication Provider .
Click Default,
Default
Step 3: Check Anonymous Access and Click OK.
Access Step 4: Open Visual Studio, create New Sharepoint Project, and add Application Page in it .
Step 5: Now remove DynamicMasterPageFile="~masterurl/default.master" from aspx page .
Step 6: Change the Code in aspx.cs to UnsecuredLayoutsPageBase as per your requirement below,
public partial class PageName: UnsecuredLayoutsPageBase
{
protected override bool AllowAnonymousAccess
{
get
{
return true;
}
}
}
Step 7: Open the Page in Browser (No Login Pop-up will come).