What is the difference between RunWithElevatedPriviledges and HostingEnvironment.Impersonate()?
-
10-12-2019 - |
Question
What is the difference between RunWithElevatedPriviledges and HostingEnvironment.Impersonate()?
Both are used to run code with higher priviledges..
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.
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())
{
}