Какую версию платформы .NET можно использовать? Поиск и поиск контента в AD стал чрезвычайно простым в .NET 3.5 - см. Этот great MSDN article от Ethan Wilanski и Joe Kaplan по использованию API принципов безопасности для этого.
Если вы еще не на .NET 3.5, вам придется использовать класс DirectorySearcher
и настроить фильтры поиска по мере необходимости. Получение права на LDAP-фильтр, вероятно, является самым большим препятствием.
Робби Аллен также имеет два больших интро статью по программированию System.DirectoryServices: - Part 1 - Part 2
Есть некоторые действительно хорошие ресурсы на http://www.directoryprogramming.net (сайте Джо Каплана - Он каталог Microsoft MVP Active), и У Ричарда Мюллера есть несколько отличных справок о том, какие свойства доступны для каждого из поставщиков ADSI, и что они означают, и как их имя LDAP - см. http://www.rlmueller.net.
Marc
EDIT: Оксенгендлер вот .NET 2.0/3.0 подход:
// set the search root - the AD container to search from
DirectoryEntry searchRoot = new DirectoryEntry("LDAP://dc=yourdomain,dc=com");
// create directory searcher
DirectorySearcher ds = new DirectorySearcher(searchRoot);
ds.SearchScope = SearchScope.Subtree;
// set the properties to load in the search results
// the fewer you load, the better your performance
ds.PropertiesToLoad.Add("cn");
ds.PropertiesToLoad.Add("sn");
ds.PropertiesToLoad.Add("givenName");
ds.PropertiesToLoad.Add("mail");
// set the filter - here I'm using objectCategory since this attribute is
// single-valued and indexed --> much better than objectClass in performance
// the "anr" is the "ambiguous name resolution" property which basically
// searches for all normally interesting name properties
ds.Filter = "(&(objectCategory=person)(anr=user-name-here))";
// get the result collection
SearchResultCollection src = ds.FindAll();
// iterate over the results
foreach (SearchResult sr in src)
{
// do whatever you need to do with the search result
// I'm extracting the properties I specified in the PropertiesToLoad
// mind you - a property might not be set in AD and thus would
// be NULL here (e.g. not included in the Properties collection)
// also, all result properties are really multi-valued, so you need
// to do this trickery to get the first of the values returned
string surname = string.Empty;
if (sr.Properties.Contains("sn"))
{
surname = sr.Properties["sn"][0].ToString();
}
string givenName = string.Empty;
if (sr.Properties.Contains("givenName"))
{
givenName = sr.Properties["givenName"][0].ToString();
}
string email = string.Empty;
if (sr.Properties.Contains("mail"))
{
email = sr.Properties["mail"][0].ToString();
}
Console.WriteLine("Name: {0} {1}/Mail: {2}", givenName, surname, email);
}
Надеется, что это помогает!
Marc
с использованием .net 3.0 я ожидал, что я использовал бы класс искателя. Я никогда не играл с AD раньше. Что я делаю? – 2009-05-23 17:01:15
спасибо marc! – 2009-05-23 17:49:01
Мне нужно импортировать что угодно, чтобы использовать запись в каталоге ??? – 2009-05-23 17:54:28