Anonymous access and elevated privileges

Accessing a SharePoint site or web with elevated privileges can be achieved in two ways. By passing a SPUserToken or with RunWithElevatedPrivileges.

string currentWebUrl = SPContext.Current.Web.Url;
SPSite _siteElevated;

SPSecurity.RunWithElevatedPrivileges(delegate { _siteElevated = new SPSite(currentWebUrl); });

_siteElevated = new SPSite(currentWebUrl, SPContext.Current.Site.SystemAccount.UserToken);

<p>
  As anonymous visitor you will not be able to access SPContext.Current.Site.SytemAccount.UserToken. So use SPSecurity to get elevated objects if you have anonymous access enabled.
</p>
Technorati Tags: