Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sharepoint 2010 使用提升权限运行的最佳实践_Sharepoint 2010 - Fatal编程技术网

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

Sharepoint 2010 使用提升权限运行的最佳实践,sharepoint-2010,Sharepoint 2010,在这两种方法中,哪一种更适合使用提升的权限运行 第一种方法: SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite curSite = new SPSite(SPContext.Current.Site.ID)) { using (SPWeb web = curSi

在这两种方法中,哪一种更适合使用提升的权限运行

第一种方法:

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();
                            }
                        }
                    }
                });
第二种方法:

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();
                            }
                        }
                    }
                });

是否有人怀疑由于外部进程异常而更改了正在更新的web?

这两种方法几乎完全相同。除了第二种方法之外,您还需要处理当前上下文的SPWeb和SPSite对象,这是您不应该做的。web.Update调用是否引发异常?问题出在“自己动手”代码中吗?

这两种方法几乎完全相同。除了第二种方法之外,您还需要处理当前上下文的SPWeb和SPSite对象,这是您不应该做的。web.Update调用是否引发异常?问题是否存在于“自己动手”代码中?

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

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

第三,当您使用using构造时,不必对using头中设置的变量调用.Dispose。实际上,使用构造的作用和实际好处是,您不必关心如何处理对象。一旦块代码退出,即使出现异常,也会对对象调用.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
    }
}
这将允许您在webapplication外部运行提升的代码

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

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";
}

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

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

第三,当您使用using构造时,不必对using头中设置的变量调用.Dispose。实际上,使用构造的作用和实际好处是,您不必关心如何处理对象。一旦块代码退出,即使出现异常,也会对对象调用.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
    }
}
这将允许您在webapplication外部运行提升的代码

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

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";
}

取决于您在更新呼叫之前所做的操作。有时,您可能需要在一系列更改中提前更新,然后获取SPWeb对象的新实例。这取决于您在更新调用之前所做的操作。有时,您可能需要在一系列更改中提前更新,然后获取SPWeb对象的新实例。首先,在提升代码之前,您应该调用SPWeb.ValidateFormDigest或SPUtility.ValidateFormDigest,我相信这只适用于POST请求,并在GET request时引发异常我假设您的代码运行在按钮后面,如果不是这样,请将其删除。因为我在using构造中调用dispose方法,这是否可能是外部进程异常更改了正在更新的web的可能原因之一?粘贴的第二个代码段可能存在此类问题,因为您正在处理当前上下文的web对象。首先,我认为这不会导致问题,因为dispose方法是在您创建的SPWEb实例上调用的。首先,关于这一点,您应该在提升代码之前调用SPWEb.ValidateFormDigest或SPUtility.ValidateFormDigest,我相信这会起作用
仅适用于POST请求,并将在GET request时引发异常我假设您的代码运行在按钮后面,如果不是这样,请将其删除。因为我在using构造中调用dispose方法,这是否可能是外部进程异常更改了正在更新的web的可能原因之一?粘贴的第二个代码段可能存在此类问题,因为您正在处理当前上下文的web对象。首先,我认为这不会导致问题,因为dispose方法是在您创建的SPWEb实例上调用的?也许这里会有些东西你在做什么?也许这里会有些东西