c# - 如何知道 DirectoryEntry 是用户还是组?

标签 c# .net active-directory

你好,

我有以下代码从当前 AD 创建树:

public static ActiveDirectory GetActiveDirectoryTree(string pathToAD = "")
{
    DirectoryEntry objADAM = default(DirectoryEntry);
    // Binding object. 
    DirectoryEntry objGroupEntry = default(DirectoryEntry);
    // Group Results. 
    DirectorySearcher objSearchADAM = default(DirectorySearcher);
    // Search object. 
    SearchResultCollection objSearchResults = default(SearchResultCollection);
    // Binding path. 
    ActiveDirectory result = new ActiveDirectory();
    ActiveDirectoryItem treeNode;

    // Get the AD LDS object. 
    try
    {
        if (pathToAD.Length > 0)
            objADAM = new DirectoryEntry();
        else
            objADAM = new DirectoryEntry(pathToAD);
        objADAM.RefreshCache();
    }
    catch (Exception e)
    {
        throw e;
    }

    // Get search object, specify filter and scope, 
    // perform search. 
    try
    {
        objSearchADAM = new DirectorySearcher(objADAM);
        objSearchADAM.Filter = "(&(objectClass=group))";
        objSearchADAM.SearchScope = SearchScope.Subtree;
        objSearchResults = objSearchADAM.FindAll();
    }
    catch (Exception e)
    {
        throw e;
    }

    // Enumerate groups 
    try
    {
        if (objSearchResults.Count != 0)
        {
            //SearchResult objResult = default(SearchResult);
            foreach (SearchResult objResult in objSearchResults)
            {
                objGroupEntry = objResult.GetDirectoryEntry();
                result.ActiveDirectoryTree.Add(new ActiveDirectoryItem() { Id = objGroupEntry.Guid, ParentId = objGroupEntry.Parent.Guid, AccountName = objGroupEntry.Name, Type = ActiveDirectoryType.Group, PickableNode = false });

                foreach (object child in objGroupEntry.Properties["member"])
                {
                    treeNode = new ActiveDirectoryItem();
                    var path = "LDAP://" + child.ToString().Replace("/", "\\/");
                    using (var memberEntry = new DirectoryEntry(path))
                    {
                        if (memberEntry.Properties.Contains("sAMAccountName") && memberEntry.Properties.Contains("objectSid"))
                        {
                            treeNode.Id = Guid.NewGuid();
                            treeNode.ParentId = objGroupEntry.Guid;
                            treeNode.AccountName = memberEntry.Properties["sAMAccountName"][0].ToString();
                            treeNode.Type = ActiveDirectoryType.User;
                            treeNode.PickableNode = true;
                            treeNode.FullName = memberEntry.Properties["Name"][0].ToString();

                            byte[] sidBytes = (byte[])memberEntry.Properties["objectSid"][0];
                            treeNode.ObjectSid = new System.Security.Principal.SecurityIdentifier(sidBytes, 0).ToString();

                            result.ActiveDirectoryTree.Add(treeNode);
                        }
                    }
                }
            }
        }
        else
        {
            throw new Exception("No groups found");
        }
    }
    catch (Exception e)
    {
        throw new Exception(e.Message);
    }

    return result;
} 

问题是使用 (var memberEntry = new DirectoryEntry(path)) 返回 DomainUsers 作为该树的用户,我不确定这是否正确?

假设我存储了 DomainUsers 节点的 sidId,然后将其发送到以下方法:

public static Boolean GetActiveDirectoryName(string sidId,out string samAccountName,out string fullName)
        {
            samAccountName = string.Empty;
            fullName = string.Empty;


            if (sidId != null && sidId.Length > 0)
            {
                var ctx = new System.DirectoryServices.AccountManagement.PrincipalContext(ContextType.Domain, null);
                using (var up = UserPrincipal.FindByIdentity(ctx, IdentityType.Sid, sidId))
                {
                    samAccountName = up.SamAccountName;
                    fullName = up.Name;

                    return true;
                }
            }
            return false;
        }

up会被置为null?如果我在 AD 中选择另一个用户,那么它就可以正常工作。我怀疑 DomainUsers 是一个组,但我如何在 DirectoryEntry 上检查它?

致以最诚挚的问候

最佳答案

我突然想到:您是否考虑过检查返回结果的架构属性?我认为您可以使用 DirectoryEntry.SchemaEntry.Name 轻松地确定一个组。如果您的架构条目是一个组,它应该返回 group

引用:MSDN: DirectoryEntry.SchemaEntry


出于好奇,上面的代码有点离题:

 if (pathToAD.Length > 0)
      objADAM = new DirectoryEntry();
 else
      objADAM = new DirectoryEntry(pathToAD);
 objADAM.RefreshCache();

如果 Length>0,你不想使用 pathToAD 吗?

关于c# - 如何知道 DirectoryEntry 是用户还是组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6354807/

相关文章:

c# - 在 C# 中使用 XSLT 将 XML 转换为 HTML 的最简单方法?

c# - Windows 服务器中的 'Microsoft.ACE.OLEDB.12.0' 提供程序未在本地计算机错误上注册

powershell - 导出/导入 AD 用户,包括管理员属性

.NET DirectoryServices 查找方法使用 UTC 还是本地时间?

c# - 为什么我的 LDAP 查询失败?

c# - 从 C# 应用程序调用 C++ DLL

c# - 使用 NodaTime 将无效(跳过)的日期时间值转换为 UTC

.net - Oracle ODP.Net 与 Entity Framework 6 - 找不到 Entity Framework 数据库兼容提供程序

c# - 选择具有所有给定标签的 EF 实体(其中标签是 EF 实体)

c# - 为 ASP.NET GridView 中的所有空单元格着色