zoukankan      html  css  js  c++  java
  • 利用RunWithElevatedPrivileges模拟管理员权限时慎用SPContext

     
     

    WSS 3.0中我们使用:

    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);
    });

    这时候即使在提升权限的范围内运行,得到的也是当前网站登录帐户名,而不是管理员登录帐号。

  • 相关阅读:
    【软件】Linux图形软件VNC&X11
    【C++语法】STL
    【C++语法】Type & Value Category
    【C++语法】关键字
    【C++语法】C++语法目录
    【算法·Algorithms】 Sort
    【代码·Patten】theme: Calculator
    centos MIT 6.828 qemu 安装问题
    【归纳】Layui table.render里的json后台传入
    【归纳】springboot中的IOC注解:注册bean和使用bean
  • 原文地址:https://www.cnblogs.com/TNSSTAR/p/2861091.html
Copyright © 2011-2022 走看看