【问题标题】:Sharepoint RunWithElevatedPrivileges vs ImpersonationSharepoint RunWithElevatedPrivileges 与模拟
【发布时间】:2012-05-16 10:36:11
【问题描述】:

我花了几个小时来解决一个奇怪的“错误”,该错误涉及来自 .NET Web 服务的请求 AD 信息用户权限的 Web 方法。

好消息是我修复了这个错误,但我明白为什么更正是有效的。

有bug的web方法如下:

public bool ValidateTask(string originatingUser)
{
    SPUserToken userToken = null;

    // get the System account for impersonation
    string userToken = site.SystemAccount.UserToken; 
    using (SPSite rootSite = new SPSite(site.ID, userToken)) 
    {
        using (SPWeb web = rootSite.OpenWeb()) 
        {
            // get the domain name of the application pool of the web app
            string servicesDomain = 
                StringUtilities.GetDomain(site.WebApplication.ApplicationPool.ManagedAccount.Username);
            // get the domain name of the user
            string accountsDomain = StringUtilities.GetDomain(originatingUser);

            PrincipalContext ServicesDomainContext = 
                new PrincipalContext(ContextType.Domain, servicesDomain);
            PrincipalContext AccountsDomainContext = 
                new PrincipalContext(ContextType.Domain, accountsDomain);

            // COMException when the FindByIdentity is called because 
            // AccountsDomainContext.connectedServer throw exception
            using (UserPrincipal usr = 
                UserPrincipal.FindByIdentity(AccountsDomainContext, IdentityType.SamAccountName, originatingUser))
            {
            // get user groups memberships
            }
        }
        // check groups memberships and return the true or false
    }
}

带校正的web方法如下:

public bool ValidateTask(string originatingUser)
{
    SPSecurity.RunWithElevatedPrivileges(
        delegate ()
        {
            ...
            using (SPSite rootSite = new SPSite(site.ID))
            {
                using (SPWeb web = rootSite.OpenWeb())
                {
                    // get the domain name of the application pool of the web app
                    string servicesDomain = 
                        StringUtilities.GetDomain(site.WebApplication.ApplicationPool.ManagedAccount.Username);
                    // get the domain name of the user
                    string accountsDomain = StringUtilities.GetDomain(originatingUser);

                    PrincipalContext ServicesDomainContext = 
                        new PrincipalContext(ContextType.Domain, servicesDomain);
                    PrincipalContext AccountsDomainContext = 
                        new PrincipalContext(ContextType.Domain, accountsDomain);

                    using (UserPrincipal usr = 
                        UserPrincipal.FindByIdentity(AccountsDomainContext, IdentityType.SamAccountName, originatingUser))
                    {
                    // get user groups memberships
                    }
                }
            }

           // check groups memberships and return the true or false
        }
    ); // end of delegate method
}

================================================ ==============================

在 sharepoint 中,我认为 Impersonation 和 RunWithElevatedPrivilege 会产生相同的结果。 所以我的问题是:

1- 那么为什么 RunWithElevatedPrivilege 有效?

2- 当我们在 WebMethod 上下文中提升权限时,凭证是什么?这是 SharePoint Web Services Root 的身份池帐户?

3- 我们可以追踪这两种方法的凭据吗?

【问题讨论】:

    标签: web-services sharepoint impersonation


    【解决方案1】:

    RunWithElevatedPrivileges 在新线程中运行代码。这个新线程在当前应用程序池的帐户下运行。如果你称之为例如http://localhost/_vti_bin/yourservice 下的应用程序池是 80 端口上的 Web 应用程序的应用程序。 使用带有用户令牌的新 SPSite 仅在已定义用户的上下文中打开 SPSite,并且不会启动新线程。 您可以通过调用 WindowsIdentity.Current 来跟踪当前用户

    【讨论】:

    • 感谢您的回复。我的 asmx 文件位于 template\layouts 文件夹中,而 dll 位于 GAC 中。用户令牌是“sharepoint\system”帐户。但是当我们使用新的 SPSite(siteId, usertoken) 进行模拟时,我们在共享点上下文中拥有完全权限,但是当我们在 AD(或文件系统)上执行查询时,WindowsIdentity 仍然是调用 Web 服务的当前用户。这就是 RunWithElevatedPrivileges 更强大的原因,因为有了 ApplicationPoolidentity 的新线程。
    猜你喜欢
    • 1970-01-01
    • 2012-01-19
    • 2010-12-04
    • 1970-01-01
    • 2011-06-02
    • 1970-01-01
    • 2014-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多