【问题标题】:SharePoint PublishingWeb change under elevated security context fails, why?在提升的安全上下文下更改 SharePoint PublishingWeb 失败,为什么?
【发布时间】:2011-04-06 11:43:33
【问题描述】:

我在更新 RunWithElevatedPrivileges 下的 SharePoint publishingWeb 属性时遇到问题。它失败,在这一行出现异常“此页面的安全验证无效”:“pubWeb.IncludeInCurrentNavigation = false;”。下面是我试图运行的代码。通常你可以设置 AllowUnsafeUpdates = true,但是 publishingWeb 没有这个特殊属性。

我的问题是在提升的上下文中更新 publishingWeb 属性的正确方法是什么?

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite siteCollection = new SPSite(parentSiteUrl))
                {
                    //siteCollection.AllowUnsafeUpdates = true;
                    using (SPWeb web = siteCollection.OpenWeb(subSiteUrl))
                    {
                        //web.AllowUnsafeUpdates = true;
                        if (PublishingWeb.IsPublishingWeb(web))
                        {
                            // hide new sub-site from navigation elements.
                            PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(web);
                            pubWeb.IncludeInCurrentNavigation = false;
                            pubWeb.IncludeInGlobalNavigation = false;
                            pubWeb.Update();
                        }
                    }
                }
            });

【问题讨论】:

  • 您的代码在哪里运行?功能接收器? Web 部件?
  • 它在用户控件中运行,该控件被烘焙到页面布局中。

标签: sharepoint elevated-privileges


【解决方案1】:

如果此更改发生在回发(POST)中,您应该在进行更改之前调用SPSecurity.ValidateFormDigest()。 AllowUnsafeUpdates 仅用于 http GET 请求。

如果它是一个 GET 请求,我原以为注释掉的行会起作用,但由于它被注释了,我认为它没有。我建议你使用:

pubWeb.Web.AllowUnsafeUpdates = true

PublishingWebSPWeb 实例的包装器,可通过 Web 属性访问。不过很奇怪,我原以为提供的 SPWeb 是同一个实例(因此您的注释行应该有效。)

【讨论】:

  • 此操作在回发时发生。鉴于此,我是否需要将 SPSecurity.ValidateFormDigest() 与 pubWeb.Web.AllowUnsafeUpdates = true 结合使用才能正常工作?
  • 没有。 ValidateFormDigest 用于 POST,AllowUnsafeUpdate 用于 GET。表单摘要的要点是它保持页面不可篡改和“安全”。获取请求被认为是不安全的,因为它很容易篡改可能在敏感方法调用中使用的查询字符串。如果您发现自己在 POST 和 GET 中进行了相同的更改,那么请重新审视您的设计。您应该只争取 POST。
  • 鉴于这些操作仅针对回发,“SPSecurity.ValidateFormDigest()”应该放在 RunWithElevatedPrivileges 块内部还是外部?
  • 在里面。但只要仔细查看您的代码 - ValidateFormDigest 仅适用于上下文网络;您正在使用显式 URL,因此我将不得不修改我的建议 - 您应该同时使用 pubWeb.Web.AllowUnsafeUpdates 和 ValidateFormDigest。前者启用对 pubwebs 的更改,后者确保当前页面未被篡改。关注?
  • 嗯,詹姆斯?任何进一步的信息?固定的?如果是这样,请将某人标记为答案(如果适用)。谢谢。
【解决方案2】:

正在阅读有关使用此属性的一些信息

pubWeb.Navigation.ExcludeFromNavigation(true, web.ID);

而不是

pubWeb.IncludeInCurrentNavigation = false;

pubWeb.IncludeInGlobalNavigation = false;

不确定这是否与您尝试完成的目标相关。

SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite siteCollection = new SPSite(parentSiteUrl))
                    {
                        //siteCollection.AllowUnsafeUpdates = true;
                        using (SPWeb web = siteCollection.OpenWeb(subSiteUrl))
                        {
                            //web.AllowUnsafeUpdates = true;
                            if (PublishingWeb.IsPublishingWeb(web))
                            {
                                // hide new sub-site from navigation elements.
                                PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(web);
                                pubWeb.Navigation.ExcludeFromNavigation(true, web.ID);
                                pubWeb.Update();
                            }
                        }
                    }
                });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-19
    • 1970-01-01
    • 2023-02-15
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    • 2012-07-27
    相关资源
    最近更新 更多