RunWithElevatedPrivileges 和 HostingEnvironment.Impersonate() 之间有什么区别?
-
10-12-2019 - |
题
RunWithElevatedPrivileges 和 HostingEnvironment.Impersonate() 之间有什么区别?
两者都用于运行具有更高权限的代码。
解决方案
Hosting.HostingEnvironment.Impersonate
ASP.NET 相当于 SPSecurity.RunWithElevatedPrivileges
.
SPSecurity.RunWithElevatedPrivileges
无法在 ASP.NET 上下文中工作,因为它调用 SharePoint API。
我有一个 ASP.NET Web 应用程序,它必须在当前用户的上下文中执行代码(通过身份模拟,因为它需要调用各种资源)。有一些代码块,例如将异常写入必须作为应用程序池帐户运行的事件日志。
在 SharePoint(其 asp.net 设置与我的 Web 应用程序非常相似)中,有 SPSecurity.RunWithElevatedPrivileges。这可确保代码块作为应用程序池帐户运行。asp.net 中的等效项如下:-
using (System.Web.Hosting.HostingEnvironment.Impersonate())
{
}