Sharepoint 2010 sharepoint2010&&引用;试图执行未经授权的操作。”&;SPListItems.roleasignments

Sharepoint 2010 sharepoint2010&&引用;试图执行未经授权的操作。”&;SPListItems.roleasignments,sharepoint-2010,Sharepoint 2010,当代码试图在事件接收器中的SPListItems.roleasignments集合中迭代时,我收到一个错误。但仅适用于具有参与者权限的用户。具有管理员权限的用户 我尝试了以下方法: 把我的秘密包裹起来 Windows Impersion使用以下代码: WindowsImpersonationContext ctx = null; ctx = WindowsIdentity.Impersonate(System.IntPtr.Zero); SPUserToken oSysToken = GetSy

当代码试图在事件接收器中的
SPListItems.roleasignments
集合中迭代时,我收到一个错误。但仅适用于具有参与者权限的用户。具有管理员权限的用户

我尝试了以下方法:

  • 把我的秘密包裹起来
  • Windows Impersion使用以下代码:

    WindowsImpersonationContext ctx = null;
    ctx = WindowsIdentity.Impersonate(System.IntPtr.Zero);
    SPUserToken oSysToken = GetSysToken(properties.SiteId)
    private static SPUserToken GetSysToken(Guid SPSiteID)
    {
        SPUserToken sysToken = null;
        using(SPSite oSite = new SPSite(SPSiteID))
        {
            sysToken = oSite.SystemAccount.UserToken;
        }
        if (sysToken == null)
        {
            SPSecurity.RunWithElevatedPrivileges(
            delegate()
            {
                using(SPSite site = new SPSite(SPSiteID))
                {
                    sysToken = site.SystemAccount.UserToken;
                }
            });
        }
        return sysToken;
    }
    
  • 最后我尝试了
    SPWeb.AllowUnsafeUpdates=true

  • 我已经尝试了所有的方法,单独和集体在一起,什么也没有。它似乎也通过了
    SPListItems.RoleAssignments.Count上的异常

    用户至少需要管理对象上的权限才能读取和修改
    SPSecurableObject.RoleAssignments

    为了在SharePoint对象上使用系统帐户(提升)权限执行代码,您必须“重新打开给定对象”:

    请注意
    SPUserToken.SystemAccount
    的用法。这使得SharePoint 2007中所需的“系统帐户令牌”攻击过时

    对于SharePoint对象上的操作
    SPSecurity.RunWithElevatedPrivileges
    WindowsIdentity.Impersonate(System.IntPtr.Zero)
    不是必需的

    我写了一篇博文,内容涉及这个主题:

    SPListItem item = // the item from the event receiver
    
    // Re-open the item with the system account
    using (SPSite adminSite = new SPSite(item.Web.Url, SPUserToken.SystemAccount))
    {
      using (SPWeb adminWeb = adminSite.OpenWeb())
      {
        SPListItem adminItem = adminWeb
          .Lists[item.ParentList.ID]
          .GetItemByUniqueId(i.UniqueId);
    
        // execute your code with the system account on the item.
        // adminItem.RoleAssignments.WhatEver
      }
    }