利用RunWithElevatedPrivileges模拟管理员权限时慎用SPContext
2011-07-14 10:43
441 查看
WSS 3.0中我们使用:
在RunWithElevatedPrivileges中不要使用SPContext.Current.Web,SPContext.Current.Site,SPControl.GetContextWeb(HttpContext.Current)之类的根据当前上下文得到当前的Web或者Site,根据这些方法得到的所有对象(包括从根据这些对象得到的List,ListItem等等对象)都是以当前网站登录用户权限运作的,即使是在RunWithElevatedPrivileges其运作权限也不会是管理员。
所以,如果要真正让在RunWithElevatedPrivileges中的代码以管理员权限正常运作的话,必须重新初始化相应的对象,比如:
SPSecurity.RunWithElevatedPrivileges(delegate() { // implementation details omitted });可以提升代码的运行权限,实现模拟管理员身份的功能。
在RunWithElevatedPrivileges中不要使用SPContext.Current.Web,SPContext.Current.Site,SPControl.GetContextWeb(HttpContext.Current)之类的根据当前上下文得到当前的Web或者Site,根据这些方法得到的所有对象(包括从根据这些对象得到的List,ListItem等等对象)都是以当前网站登录用户权限运作的,即使是在RunWithElevatedPrivileges其运作权限也不会是管理员。
所以,如果要真正让在RunWithElevatedPrivileges中的代码以管理员权限正常运作的话,必须重新初始化相应的对象,比如:
SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite mySite = new SPSite(SPContext.Current.Site.Url)) { Response.Write(mySite.RootWeb.CurrentUser.LoginName); } });
以上mySite.RootWeb.CurrentUser.LoginName返回的是管理员的登录帐号。
但是如果按之前所说使用SPContext:
SPSecurity.RunWithElevatedPrivileges(delegate() { Response.Write(SPContext.Current.Web.CurrentUser.LoginName); });这时候即使在提升权限的范围内运行,得到的也是当前网站登录帐户名,而不是管理员登录帐号。
相关文章推荐
- 利用RunWithElevatedPrivileges模拟管理员权限时慎用SPContext
- 利用RunWithElevatedPrivileges模拟管理员权限时慎用SPContext
- SharePoint v3:忘掉模拟用户Impersonate,SPSecurity.RunWithElevatedPrivileges来了 提升SharePoint代码的权限
- SharePoint 2010 权限提升-SPSecurity.RunWithElevatedPrivileges method (Microsoft.SharePoint)
- MOSS: SPSecurity.RunWithElevatedPrivileges提升权限来新增列表条目示例
- SharePoint v3:忘掉模拟用户Impersonate,SPSecurity.RunWithElevatedPrivileges来了
- SharePoint v3:忘掉模拟用户Impersonate,SPSecurity.RunWithElevatedPrivileges来了
- MOSS: SPSecurity.RunWithElevatedPrivileges提升权限来新增列表条目示例
- All ways of RunWithElevatedPrivileges
- 正确使用RunWithElevatedPrivileges
- sharepoint 一个有用的方法SPSecurity.RunWithElevatedPrivileges(delegate(){})
- 【原创】关于SPSecurity.RunWithElevatedPrivileges的一个问题[A problem about SPSecurity.RunWithElevatedPrivileges]
- SPSecurity.RunWithElevatedPrivileges exception 80040154
- 当使用RunWithElevatedPrivileges时如何修改“修改者”信息
- ATL项目编译注册dll的时候报权限错误:error MSB8011: Failed to register output. Please try enabling Per-user Redirection or register the component from a command prompt with elevated permissions.
- vs2005 生成文件与win7兼容 \ 利用manifest提升程序为WIN7管理员权限
- RunAsAdmin解决方案 提供regedit的管理员权限读写
- 利用ShellExecuteEx手动提升用户特权,以管理员权限来运行程序。win7会有UAC弹窗
- MOSS模拟管理员权限
- 如何在程序里模拟在cmd里用管理员权限运行一条指令