Programming with Anonymous Accessable Portals

Let's say, you have a portal that can be accessed with anonymous access and the anonymous users will reach a data on SharePoint that anonymous users can not reach. You can use the code below to accomplish this:

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    SPSite sc = new SPSite(SPContext.Current.Site.ID);
    foreach (SPUser user in sc.RootWeb.Groups["Portal Owners"].Users)
    {
        Response.Write(user.Name + "<br />");
    }
});

The RunWithElevatedPrivileges method makes the code run with the account which is the application pool identity of the web application the code runs in.

Leave a Reply