【问题标题】:SharePoint 2010 Change context for RunWithElevatedPrivilegesSharePoint 2010 更改 RunWithElevatedPrivileges 的上下文
【发布时间】:2012-01-19 17:54:10
【问题描述】:

我有 2 个网站集,我们只称它们为 A 和 B。在 A 中,我有一个我想跨多个网站集同步的一些内容的列表。我创建了一个自定义内容查询 Web 部件来查询数据,但我遇到了障碍。

由于用户级帐户根本无权访问站点 A,并且我不希望他们在任何级别访问此站点,因此当我的 Web 部件在站点 B 上运行时出现访问被拒绝错误. 有没有办法改变 RunWithElevatedPrivileges 工作的上下文?

我当前从网站集 B 运行的代码看起来像

ClientContext clientContext = new ClientContext(siteAUrl);
SP.List oList = clientContext.Web.Lists.GetByTitle(listName);

CamlQuery query = new CamlQuery();
//Add View, Execute Query, etc.

return results;

如果我将它包装在 SPSecurity.RunWithElevatedPrivileges 中,它会在网站集 B 上下文中以提升的方式运行,而我希望它在网站集 A 上下文中运行。

【问题讨论】:

    标签: sharepoint sharepoint-2010 sitecollection


    【解决方案1】:

    SharePoint 2010 客户端对象模型不支持SPSecurity.RunWithElevatedPrivileges。正如@Nat 所述,此调用将线程标识恢复为应用程序池帐户(SystemAccount)。由于您的代码在客户端上运行,因此无法将身份恢复到应用程序池帐户。 通过客户端对象模型将代码作为系统帐户运行会打开一个安全漏洞。每个人都可以作为管理员编写和执行代码。

    如果您的代码在服务器上运行,您可以使用SPUserToken.SystemAccount 使用系统帐户凭据打开SPSite

    using (SPSite site = new SPSite("http://someurl", SPUserToken.SystemAccount))
    {
      // admin action here
    }
    

    查看我的博文How to Open a SPSite with System Account Credentials 了解更多信息。

    【讨论】:

      【解决方案2】:

      请记住,SPSecurity.RunWithElevatedPrivileges 正在应用程序池帐户的安全上下文中运行代码。 因此,除非您在不同的 Web 应用程序中运行网站集,否则它们将具有相同的上下文。即所有被调查的主人。

      如果您跨网站集运行,应用程序池帐户将规定其他 Web 应用程序的权限。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-05-16
        • 2011-07-19
        • 1970-01-01
        • 2010-12-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-09
        相关资源
        最近更新 更多