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