我正在尝试修复一些将项目插入 SharePoint 列表的代码(我没有编写)。问题是代码适用于匿名用户,但如果用户通过 ASP.NET 表单身份验证登录,则在调用 SPListItem 的 Update 方法时会收到 UnauthorizedAccessException。当它工作时,作为匿名用户,我可以看到 SPListItem 的 SPWeb 的 SPUser 是 SharePoint 系统帐户。但是当用户使用表单身份验证登录时,SPUser 为空。有人可以解释这种行为以及如何解决它吗?
最初只有最上面的代码块位于 RunWithElevatedPrivileges 委托中,但我尝试将其全部移到内部。一旦我开始工作,我将插入一些 using 块:
SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            rootWeb = SPContext.Current.Site.RootWeb;
            rootWeb.AllowUnsafeUpdates = true;
            currentWeb = SPContext.Current.Web;
            currentWeb.AllowUnsafeUpdates = true;
        try
        {
            // Get site information
            SPList franDir = rootWeb.GetList("/Lists/Directory");
            SPQuery query = new SPQuery();
            query.Query = "<Where><Eq><FieldRef Name='Subsite'/><Value Type='Text'>" + currentWeb.Name +
                 "</Value></Eq></Where>";
            SPListItemCollection items = franDir.GetItems(query);
            SPList l = rootWeb.GetList("/Lists/Request");
            SPListItem li = l.Items.Add();
            li["Location"] = siteName;
            //...set more fields
            li.Update();
        }
        catch (Exception ex)
        {
            rootWeb.Dispose();
            logger.ErrorException("An error occured adding item", ex);
            throw ex;
        }
        rootWeb.Dispose();
        });