连接到Active Directory以使用C#检索用户详细信息

yoh*_*hna 1 active-directory visual-studio-2010 c#-4.0

我发现了一篇关于从这里获取活动目录用户的文章

所以我的代码可能会喜欢这个

String strPath = "format of this path";
DirectoryEntry entry = null;
entry = new DirectoryEntry(strPath);

DirectorySearcher mySearcher = new DirectorySearcher(entry);

mySearcher.Filter = ("ObjectCategory=user");

foreach (SearchResult result in mySearcher.FindAll())
{
    String strName = result.GetDirectoryEntry().Name;
    //Do whatever
}
Run Code Online (Sandbox Code Playgroud)

你能解释一下strPath吗?什么是这种格式?

注意:我知道我的服务器信息可以使用我的本地IP"198.168.1.182"进行测试.

我不确定我的想法是否正确.

请帮忙 !!!

mar*_*c_s 5

由于您使用的是.NET 4,因此您应该查看System.DirectoryServices.AccountManagement(S.DS.AM)命名空间.在这里阅读所有相关内容:

管理.NET Framework 3.5中的目录安全性主体

基本上,您可以定义域上下文并轻松查找AD中的用户和/或组:

// set up domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

// find user by name
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "John Doe");

// if we found user - inspect its details
if(user != null)
{
    string firstName = user.GivenName;
    string lastName = user.Surname;
    string email = user.EmailAddress;
    string phone = user.VoiceTelephoneNumber;
}
Run Code Online (Sandbox Code Playgroud)

新的S.DS.AM使得在AD中使用用户和群组变得非常容易: