从LDAP确定给定id的objectClass

时间:2013-02-07 14:36:41

标签: c# active-directory ldap

获取给定ID的objectClass名称的最佳方法是什么。

我的输入应该是“IDNSKF” 我的输出应该是组或人或计算机..

我使用以下代码,但有时在distinguishedName我找不到OU=Groups但是对象是组

代码:

DirectoryEntry de = new DirectoryEntry("LDAP://local.be");
        DirectorySearcher searcher = new DirectorySearcher(de);
        searcher.Filter = "(&(cn=variable))";
        searcher.SearchScope = SearchScope.Subtree;
        searcher.PropertiesToLoad.Add("distinguishedName");
        SearchResult result = searcher.FindOne();
        string s = result.GetDirectoryEntry().Properties["distinguishedName"].Value.ToString();
        if (s.IndexOf("OU=Groups") > -1)
            return Groups;
        if (s.IndexOf("OU=Users") > -1)
            return Users;
        if (s.IndexOf("OU=Computers") > -1)
            return Computer;

由于

1 个答案:

答案 0 :(得分:1)

我想与您分享我找到的解决方案:

SearchResult result = directorySearcher.FindOne();
        if (result == null)
            return new LocalPermissionEntry(accessRule);

        ResultPropertyValueCollection userValueCollection = result.Properties["objectClass"];

        // check if the entry is a group or a user.
        if (userValueCollection.Contains("group"))
            return new GroupPermissionEntry(accessRule);
        if (userValueCollection.Contains("person") || userValueCollection.Contains("user"))
            return new UserPermissionEntry(accessRule);

        return new LocalPermissionEntry(accessRule);