如何在ActiveDirectory和.NET 3.5中确定用户所属的所有组(包括嵌套组)

24

我有一个应用程序使用ActiveDirectory授权,现决定需要支持嵌套的AD组,例如:

MAIN_AD_GROUP
     |
     |-> SUB_GROUP
              | 
              |-> User
所以,用户并非直接成员MAIN_AD_GROUP。我希望能够递归查找搜索嵌套在MAIN_AD_GROUP中的组来查找用户。
主要问题是我正在使用.NET 3.5,并且在.NET 3.5中的System.DirectoryServices.AccountManagement存在一个错误,即UserPrincipal.IsMemberOf()方法无法为具有超过1500个用户的组工作。因此,我无法使用UserPrincipal.IsMemberOf()方法,也不能切换到.NET 4。
我通过以下函数解决了这个问题:
private bool IsMember(Principal userPrincipal, Principal groupPrincipal)
{
    using (var groups = userPrincipal.GetGroups())
    {
        var isMember = groups.Any(g => 
            g.DistinguishedName == groupPrincipal.DistinguishedName);
        return isMember;
    }
}

userPrincipal.GetGroups() 只返回用户是直接成员的组。

如何使用嵌套组使其工作?

4个回答

34

解决方法 #1

Microsoft Connect上报告了此漏洞,同时提供以下代码来通过手动迭代返回的PrincipalSearchResult<Principal>对象并捕获此异常进行继续处理以避免此问题:

PrincipalSearchResult<Principal> groups = user.GetAuthorizationGroups();
var iterGroup = groups.GetEnumerator();
using (iterGroup)
{
    while (iterGroup.MoveNext())
    {
        try
        {
            Principal p = iterGroup.Current;
            Console.WriteLine(p.Name);
        }
        catch (NoMatchingPrincipalException pex)
        {
            continue;
        }
    }
}

解决方法 #2

另一个解决方法在这里,避免使用AccountManagement类,而是使用System.DirectoryServices API:

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.DirectoryServices;  

namespace GetGroupsForADUser  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            String username = "Gabriel";  

            List<string> userNestedMembership = new List<string>();  

            DirectoryEntry domainConnection = new DirectoryEntry(); // Use this to query the default domain
            //DirectoryEntry domainConnection = new DirectoryEntry("LDAP://example.com", "username", "password"); // Use this to query a remote domain

            DirectorySearcher samSearcher = new DirectorySearcher();  

            samSearcher.SearchRoot = domainConnection;  
            samSearcher.Filter = "(samAccountName=" + username + ")";  
            samSearcher.PropertiesToLoad.Add("displayName");  

            SearchResult samResult = samSearcher.FindOne();  

            if (samResult != null)  
            {  
                DirectoryEntry theUser = samResult.GetDirectoryEntry();  
                theUser.RefreshCache(new string[] { "tokenGroups" });  

                foreach (byte[] resultBytes in theUser.Properties["tokenGroups"])  
                {  
                    System.Security.Principal.SecurityIdentifier mySID = new System.Security.Principal.SecurityIdentifier(resultBytes, 0);  

                    DirectorySearcher sidSearcher = new DirectorySearcher();  

                    sidSearcher.SearchRoot = domainConnection;  
                    sidSearcher.Filter = "(objectSid=" + mySID.Value + ")";  
                    sidSearcher.PropertiesToLoad.Add("distinguishedName");  

                    SearchResult sidResult = sidSearcher.FindOne();  

                    if (sidResult != null)  
                    {  
                        userNestedMembership.Add((string)sidResult.Properties["distinguishedName"][0]);  
                    }  
                }  

                foreach (string myEntry in userNestedMembership)  
                {  
                    Console.WriteLine(myEntry);  
                }  

            }  
            else 
            {  
                Console.WriteLine("The user doesn't exist");  
            }  

            Console.ReadKey();  

        }  
    }  
}  

好的帖子。Workaround #1在iterGroup.MoveNext()时失败,出现相同的错误“服务器上没有这样的对象”。 - Kjensen
解决方法2只能在从已登录域的计算机上运行程序时才能工作。 如果您从不同的域查询ldap,则无法工作。 - Ronen Festinger
@RonenFestinger 有什么解决方法吗?#3 - Kiquenet
1
@Kiquenet和@Ronen,看起来你们可以使用另一个构造函数DirectoryEntry domainConnection,通过传递在不同域上有效的用户名和密码。请参阅:http://stackoverflow.com/questions/9362724/cross-domain-authentication-using-directoryservices - Tim Lewis
1
我编辑了答案,加入了一行代码,可用于连接到远程域:DirectoryEntry domainConnection = new DirectoryEntry("LDAP://example.com", "username", "password"); // Use this to query a remote domain - Tim Lewis
显示剩余2条评论

15

使用UserPrincipal.GetAuthorizationGroups()代替 - 从其MSDN文档:

该方法递归搜索所有组并返回用户所属的组。返回的集合还可以包括系统认为用户属于授权目的的其他组。

此方法返回的组可能包括来自不同作用域和存储的组。例如,如果主体是具有DN为“CN=SpecialGroups,DC=Fabrikam,DC=com”的AD DS对象,则返回的集合可能包含属于“CN=NormalGroups,DC =Fabrikam,DC=com”的组。


1
谢谢,但不幸的是,这会抛出一个PrincipalOperationException异常,其中包含消息“服务器上没有这样的对象”。UserPrincipal肯定存在,因为我上面的方法确实返回了顶级群组的适当授权。 - Sergi Papaseit
2
如果用户仍然是已删除组的成员,则“UserPrincipal.GetAuthorizationGroups()”会出现问题。 - jproch
只需要检查 group.Name 是否不为 null,在这种情况下 @jproch - Elger Mensonides

7

我知道这是一个旧帖子,但它是谷歌的最佳搜索结果,所以如果有人需要,这里是我使用AccountManagement工具得出的更简便的查询。

public static class AccountManagementExtensions
{
    public static bool IsNestedMemberOf(this Principal principal, GroupPrincipal group)
    {
        // LDAP Query for memberOf Nested 
        var filter = String.Format("(&(sAMAccountName={0})(memberOf:1.2.840.113556.1.4.1941:={1}))",
                principal.SamAccountName,
                group.DistinguishedName
            );

        var searcher = new DirectorySearcher(filter);

        var result = searcher.FindOne();

        return result != null;
    }
}

0

高效的方法是通过使用正确的DirectorySearcher过滤器进行单个AD查询。

public bool CheckMemberShip(string userName)
    {

        bool membership = false;
        string connection = "LDAP://"+YOURDOMAIN;
        DirectoryEntry entry = new DirectoryEntry(connection);
        DirectorySearcher mySearcher = new DirectorySearcher(entry);
        mySearcher.Filter = "(&(objectClass=user)(memberOf:1.2.840.113556.1.4.1941:=cn=GROUPNAME,OU=Groups,OU=ABC,OU=ABC,OU=IND,DC=ad,DC=COMPANY,DC=com)(|(sAMAccountName=" + userName + ")))";
        SearchResult result = mySearcher.FindOne();

        // No search result, hence no membership
        if (result == null)
        {
            membership = false;
        }

        entry.Close();
        entry.Dispose();
        mySearcher.Dispose();

        membership = true;
        return membership;
    }

您需要使用您的AD中正确的值替换YOURDOMAIN和GROUPNAME。

来源:如何使用.NET/C#和LDAP递归获取用户在Active Directory中的组成员身份(而不仅仅是2次访问Active Directory)

需要包括using System.DirectoryServices;


1
只要在返回语句之前有一行“membership=true”,那么这个答案将始终返回true。 - wojtow

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接