简体   繁体   English

LDAP 在 C# 中按属性搜索用户

[英]LDAP search user by attribute in C#

I have a LDAP directory http://btechintegrator.com/index.php/2020/01/22/free-online-cloud-ldap/ where i wanted to find the user with custom attribute.我有一个 LDAP 目录http://btechintegrator.com/index.php/2020/01/22/free-online-cloud-ldap/我想在其中找到具有自定义属性的用户。

I have following for getting all users我有以下获得所有用户

public List<Dictionary<string, string>> search(string baseDn, string ldapFilter)
        {          

            var result = new List<Dictionary<string, string>>();
            var request = new SearchRequest(baseDn, ldapFilter, SearchScope.Subtree);
            if (request != null)
            {
                var response = (SearchResponse)connection.SendRequest(request);

                foreach (SearchResultEntry entry in response.Entries)
                {
                    var dic = new Dictionary<string, string>();
                    dic["DN"] = entry.DistinguishedName;

                    foreach (string attrName in entry.Attributes.AttributeNames)
                    {
                        //For simplicity, we ignore multi-value attributes
                        dic[attrName] = string.Join(",", entry.Attributes[attrName].GetValues(typeof(string)));
                    }

                    result.Add(dic);
                }
            }
            return result;
        }

and I am calling like我打电话给

  var searchResult = client.search("ou=users,o=freeguests,dc=btechsample,dc=com", "objectClass=*");

Any idea what change i could do to get user which are having mobile attribute or the user which are having mobile attribute with certain value知道我可以做些什么改变来获取具有移动属性的用户或具有特定值的移动属性的用户

You can use the following :您可以使用以下内容:

  • mobile=* : get entries having the mobile attribute with any value mobile=* : 获取具有任何值的mobile属性的条目
  • mobile=0123 : get entries having mobile value 0123 mobile=0123 :获取具有移动值0123条目

Then, to filter entries with both objectClass AND mobile attributes, use the following syntax :然后,要过滤具有 objectClass 和 mobile 属性的条目,请使用以下语法:

(&(objectClass=*)(mobile=*))

You might want to specify objectClass=User to ensure you get only User entries (though the objectClass filter may not be necessary at all if the mobile attribute is available only for entries having objectClass=User, but I'm not sure of that, ie. whether or not non-User entries can have a mobile attribute).您可能希望指定objectClass=User以确保仅获得 User 条目(尽管如果 mobile 属性仅适用于具有 objectClass=User 的条目,则可能根本不需要 objectClass 过滤器,但我不确定这一点,即. 非用户条目是否可以具有移动属性)。

Also, you can define which attributes you need to read from the search results, for example :此外,您可以定义需要从搜索结果中读取哪些属性,例如:

string[] attrs = new string[] { "dn", "uid", "cn", "mobile" };
var request = new SearchRequest(baseDn, ldapFilter, SearchScope.Subtree, attrs);

You can also specify "*" to read all non-operational attributes, and/or "+" for operational attributes.您还可以指定"*"来读取所有非操作属性,和/或"+"读取操作属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM