Active directory 对active directory进行编程

Active directory 对active directory进行编程,active-directory,Active Directory,我有一个asp应用程序正在运行,但我想搜索Active Directory 我正在使用vb(VisualWebDeveloper2008) 如何在active directory中搜索给定用户 ie:用户在文本框中输入登录名,单击提交。在单击时搜索此用户的active directory。当显示找到的用户信息时 谢谢您可以使用什么版本的.NET framework?在.NET3.5中,在AD中搜索和查找内容变得非常容易——请参阅EthanWilanski和JoeKaplan关于使用安全主体API

我有一个asp应用程序正在运行,但我想搜索Active Directory

我正在使用vb(VisualWebDeveloper2008)

如何在active directory中搜索给定用户

ie:用户在文本框中输入登录名,单击提交。在单击时搜索此用户的active directory。当显示找到的用户信息时


谢谢

您可以使用什么版本的.NET framework?在.NET3.5中,在AD中搜索和查找内容变得非常容易——请参阅EthanWilanski和JoeKaplan关于使用安全主体API实现这一点的文章

如果您尚未使用.NET 3.5,则必须使用
DirectorySearcher
类并根据需要设置搜索筛选器。正确使用LDAP筛选器可能是最大的障碍

Robbie Allen还有两篇关于System.DirectoryServices编程的精彩介绍文章: - -

在(Joe Kaplan的网站-他是Microsoft Active Directory MVP)上有一些非常好的资源,Richard Mueller有一些很好的excel参考表,介绍了每个ADSI提供商的可用属性、它们的含义以及它们的LDAP名称-请参阅

马克

编辑:确定-以下是.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

使用.NET3.0我预计我将使用searcher类。我以前从未玩过广告。我该怎么办?啊,是的-您必须添加一个对“System.DirectoryServices”的.NET程序集引用,并在代码中添加一个“using System.DirectoryServices”(或VB.NET中的任何内容)。