DC是您的域名。如果你想连接到域的example.com比你的直流是:DC =例如,DC = com
你实际上不需要你的域控制器的任何主机名或IP地址(可能有很多) 。
只是您要连接到域本身的映像。因此,要连接到域example.com,您只需编写
DirectoryEntry directoryEntry = new DirectoryEntry("LDAP://example.com");
然后您就完成了。
您也可以指定用户和用于连接密码:
DirectoryEntry directoryEntry = new DirectoryEntry("LDAP://example.com", "username", "password");
而且一定要经常写LDAP大写。我遇到了一些麻烦和奇怪的例外,直到我在某处读到我应该尝试用大写字母写出来并解决了我的问题。
directoryEntry.Path
属性允许您更深入地了解您的域。所以如果你想在一个特定的OU(组织单元)中搜索一个用户,你可以在那里设置它。
DirectoryEntry directoryEntry = new DirectoryEntry("LDAP://example.com");
directoryEntry.Path = "LDAP://OU=Specific Users,OU=All Users,OU=Users,DC=example,DC=com";
这将匹配以下AD层次结构:
只需层次写从最深到最高。
Now you can do plenty of things
例如,通过账户名搜索用户,并得到其姓:
DirectoryEntry directoryEntry = new DirectoryEntry("LDAP://example.com");
DirectorySearcher searcher = new DirectorySearcher(directoryEntry) {
PageSize = int.MaxValue,
Filter = "(&(objectCategory=person)(objectClass=user)(sAMAccountName=AnAccountName))"
};
searcher.PropertiesToLoad.Add("sn");
var result = searcher.FindOne();
if (result == null) {
return; // Or whatever you need to do in this case
}
string surname;
if (result.Properties.Contains("sn")) {
surname = result.Properties["sn"][0].ToString();
}
ou =组织单位,dc =域组件 – paul 2013-02-11 13:58:46