Question

What is the difference between RunWithElevatedPriviledges and HostingEnvironment.Impersonate()?

Both are used to run code with higher priviledges..

Was it helpful?

Solution

Hosting.HostingEnvironment.Impersonate is the ASP.NET equivalent to SPSecurity.RunWithElevatedPrivileges.

SPSecurity.RunWithElevatedPrivileges cannot work in ASP.NET context as it calls for SharePoint APIs.

Source

I have an asp.net web app that must execute code under the context of the current user (via identity impersonate, as it needs to call out to various resources). There are some blocks of code, such as writing exceptions to the event log that must run as the app pool account.

In SharePoint, (which has asp.net set up very similar to my web app), there is SPSecurity.RunWithElevatedPrivileges. This ensure a block of code runs as the app pool account. The equivalent in asp.net is as follows:-

using (System.Web.Hosting.HostingEnvironment.Impersonate()) 
{ 
}

Source

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top