首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >active directory编程

active directory编程
EN

Stack Overflow用户
提问于 2009-05-23 16:53:08
回答 1查看 2.5K关注 0票数 1

我已经运行了一个asp应用程序,但我想搜索活动目录。

我使用的是vb (visual web developer 2008)

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

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

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2009-05-23 16:57:30

您可以使用哪个版本的.NET框架?在.NET 3.5中,在AD中搜索和查找内容已经变得非常容易-请参阅Ethan Wilanski和Joe Kaplan关于使用安全原则great MSDN article的这篇文章。

如果你还没有使用.NET 3.5,你将不得不使用DirectorySearcher类,并根据需要设置搜索过滤器。获得正确的LDAP过滤器可能是最大的障碍。

Robbie Allen还有两篇关于System.DirectoryServices编程的入门文章:- Part 1 - Part 2

http://www.directoryprogramming.net上有一些非常好的资源(Joe Kaplan的站点-他是Microsoft Active Directory MVP),Richard Mueller有一些很好的参考excel表,关于每个ADSI提供者可用的属性,它们的含义,以及它们的LDAP名称是什么-请参阅http://www.rlmueller.net

Marc

编辑: Ok-以下是.NET 2.0 / 3.0方法:

代码语言:javascript
复制
// 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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/901914

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档