使用提升权限运行的最佳实践

问题描述 投票:1回答:2

在两种方法中哪一种是首选的,具有提升权限的运行?

第一种方法:

SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite curSite = new SPSite(SPContext.Current.Site.ID))
                    {
                        using (SPWeb web = curSite.OpenWeb(SPContext.Current.Web.ID))
                        {
                            try
                            {
                                web.AllowUnsafeUpdates = true;
                                \\ do your stuff
                            }
                            catch (Exception e)
                            {

                            }
                            finally
                            {
                                web.AllowUnsafeUpdates = false;
                                web.Dispose();
                            }
                        }
                    }
                });

第二种方法:

SPSite oSite = SPContext.Current.Site;
SPWeb oWeb = SPContext.Current.Web;
SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite curSite = new SPSite(oSite.ID))
                    {
                        using (SPWeb web = curSite.OpenWeb(oWeb.ID))
                        {
                            try
                            {
                                web.AllowUnsafeUpdates = true;
                                \\ do your stuff
                            }
                            catch (Exception e)
                            {

                            }
                            finally
                            {
                                web.AllowUnsafeUpdates = false;
                                web.Dispose();
                                oWeb.Dispose();
                                oSite.Dispose();
                            }
                        }
                    }
                });

是否有人怀疑“正在更新的网页被外部进程更改”例外?

sharepoint-2010
2个回答
6
投票

首先,你应该在提升代码之前调用SPWeb.ValidateFormDigest()SPUtility.ValidateFormDigest()。这将消除GET请求不允许的不安全更新,并避免您设置AllowUnsafeUpdate属性。

其次,正如Nigel Whatling所提到的,你在第二个代码中处理上下文对象。你不必处置它们。简单来说,只处理你自己实例化的对象。像您这样的代码可能会导致副作用,因为其他SharePoint组件可能需要访问SPContext.Current.XX对象。这可能是您问题的根源。

第三,当您使用using构造时,您不必对使用标头中设置的变量调用.Dispose()。实际上,using构造的作用(实际上是好处)是你不必关心处理对象。一旦块代码退出,即使存在异常,也会在对象上调用.Dispose()方法。

总而言之,您的代码应更改为:

SPUtility.ValidateFormDigest();
SPSecurity.RunWithElevatedPrivileges(delegate()
{
    using (SPSite curSite = new SPSite(SPContext.Current.Site.ID))
    {
        using (SPWeb web = curSite.OpenWeb(SPContext.Current.Web.ID))
        {
              // Do stuff here
        }
    }
});

旁注:要提升代码,您有两种选择。你在这里使用的那个(调用SPSecurity.RunWithElevatedPrivileges)或使用SystemAccount标记实例化一个新的SPSite:

using (SPSite curSite = new SPSite(
    SPContext.Current.Site.ID, 
    SPContext.Current.Site.SystemAccount.UserToken
))
{
    using (SPWeb web = curSite.OpenWeb(SPContext.Current.Web.ID))
    {
          // Do stuff here
    }
}

这将允许您在Web应用程序外部运行提升的代码。

您还应该考虑使用一些实用程序代码以更实用的方式包装此类操作。我习惯使用这样的代码:

public static void RunWithElevatedPrivileges(this SPWeb web, Action<SPSite, SPWeb> codeToRunElevated)
{
    if (CheckIfElevated(web))
    {
        codeToRunElevated(web.Site, web);
    }
    else
    {
        using (var elevatedSite = new SPSite(web.Site.ID, web.AllUsers["SHAREPOINT\\system"].UserToken))
        {
            using (var elevatedWeb = elevatedSite.OpenWeb(web.ID))
            {
                codeToRunElevated(elevatedSite, elevatedWeb);
            }
        }
    }
}

/// <summary>
/// Indicates whether the context has been elevated
/// </summary>
public static bool CheckIfElevated(SPWeb web)
{
    return web.CurrentUser.LoginName == "SHAREPOINT\\system";
}

使用这样的代码,您可以在代码中的某处执行:

SPContext.Current.Web.RunWithElevatedPrivileges((elevatedSite, elevatedWeb)=>{
     // do something will all privileges
});

1
投票

两种方法几乎完全相同。除了在第二种方法中,您还要处理当前上下文的SPWeb和SPSite对象 - 这是您不应该做的事情。是否会在web.Update()调用上抛出异常?这个问题是在“做你的东西”代码中的某个地方吗?

© www.soinside.com 2019 - 2024. All rights reserved.