Sharepoint 正在使用网站集检查用户的权限

Sharepoint 正在使用网站集检查用户的权限,sharepoint,permissions,Sharepoint,Permissions,我想检查用户是否有权限访问网站集。但是我不知道如何使用SPSite.DoesUserHavePermissions() 什么是SPReusableAcl?检查用户权限时如何获取它?MSDN文章()对您没有帮助吗?示例代码可用于检查用户是否有权访问网站集: using System; using Microsoft.SharePoint; namespace Test { class Program { static void Main(string[] args

我想检查用户是否有权限访问网站集。但是我不知道如何使用
SPSite.DoesUserHavePermissions()

什么是
SPReusableAcl
?检查用户权限时如何获取它?

MSDN文章()对您没有帮助吗?示例代码可用于检查用户是否有权访问网站集:

using System;
using Microsoft.SharePoint;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("http://localhost"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    // Make sure the current user can enumerate permissions.
                    if (web.DoesUserHavePermissions(SPBasePermissions.EnumeratePermissions))
                    {
                        // Specify the permission to check.
                        SPBasePermissions permissionToCheck = SPBasePermissions.ManageLists;
                        Console.WriteLine("The following users have {0} permission:", permissionToCheck);

                        // Check the permissions of users who are explicitly assigned permissions.
                        SPUserCollection users = web.Users;
                        foreach (SPUser user in users)
                        {
                            string login = user.LoginName;
                            if (web.DoesUserHavePermissions(login, permissionToCheck))
                            {
                                Console.WriteLine(login);
                            }
                        }
                    }
                }
            }
            Console.ReadLine();
        }
    }
}
在上面的示例代码中,您只需更改站点URL和变量
permissionToCheck
。SPBasePermissions有许多可能的权限可供检查,您可以在此处看到枚举()


实际上,有很多关于如何检查用户权限的教程,您不局限于
doesherhavepermissions
,请参见以下内容。

与往常一样,MSDN示例提供了很好的教科书示例,并不总是适用于实际场景

在SharePoint 2010上运行的应用程序页面的上下文中,据我所知,此代码需要包装在对RunWithElevatedPrivileges的调用中,即使如此,正如我的评论所暗示的,似乎在需求中有一个隐含的第22条军规。这对我来说很有用(登录名只是FBA用户名或站点广告用户的“域\用户”——在我们的例子中,使用的是电子邮件地址):


SPSite.DoesUserHavePermissions(SPReusableAcl、SPBasePermissions)

我猜您的代码只是检查用户是否有访问顶级网站的权限,而不是访问网站集的权限。顶级网站!=网站集。我说得对吗?顶级网站==网站集-但现在这并不重要;-)如果要检查不是根目录的网站集/子网站的权限,只需编辑URL(如我所述)
new SPSite(“http://localhost“
此URL也可以是
”http://mysharepointweb/sites/mySiteCollection“
您能再详细说明一下什么是SPReusableAcl吗?可重用ACL:使用SPWeb、SPList或SPListItem类的ReusableAcl属性返回网站、列表或列表项的ACL。对于实现ISecurableObject接口的任何类,都可以使用ReusableAcl属性返回ACL。要返回网站集的ACL,请使用SPSite类的GetReusableAclForScope或GetAllReusableAcls方法。“
SPSecurity.RunWithElevatedPrivileges(delegate()
{
    using (SPSite elevatedSite = new SPSite(siteCollectionUrl))
    {
        foreach (SPSite siteCollection in elevatedSite.WebApplication.Sites)
        {
            using (SPWeb elevatedWeb = siteCollection.OpenWeb())
            {
                bool allowUnsafeUpdates = elevatedWeb.AllowUnsafeUpdates;
                bool originalCatchValue = SPSecurity.CatchAccessDeniedException;
                SPSecurity.CatchAccessDeniedException = false;

                try
                {
                    elevatedWeb.AllowUnsafeUpdates = true;

                    // You can't verify permissions if the user does not exist and you
                    // can't ensure the user if the user does not have access so we
                    // are stuck with a try-catch
                    SPUser innerUser = elevatedWeb.EnsureUser(loginName);

                    if (null != innerUser)
                    {
                        string splogin = innerUser.LoginName;
                        if (!string.IsNullOrEmpty(splogin) && elevatedWeb.DoesUserHavePermissions(splogin, SPBasePermissions.ViewPages))
                        {
                            // this user has permissions; any other login - particularly one that
                            // results in an UnauthorizedAccessException - does not
                        }
                    }
                }
                catch (UnauthorizedAccessException)
                {
                    // handle exception
                }
                catch (Exception)
                {
                    // do nothing
                }
                finally
                {
                    elevatedWeb.AllowUnsafeUpdates = allowUnsafeUpdates;
                    // reset the flag
                    SPSecurity.CatchAccessDeniedException = originalCatchValue;
                }
            }
        }
    }
});