C#在AD中搜索用户 [英] C# search for user in AD

查看:287
本文介绍了C#在AD中搜索用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是天生的程序员,所以我先向您道歉。我进行了广泛的搜索,发现了10种不同的方法来完成一件事情。我想做的事情看起来很简单,但我却很想念...我需要使用名字和姓氏搜索Active Directory,并在列表框中显示所有匹配的用户。有人可以指出我正确的方向,还是有人已经问过相同的问题,将我链接到该方向?

I'm not a programmer by nature so I apologize in advance. I've searched far and wide and have found bits and pieces of 10 different ways to do one thing. What I'm trying to do seems very simple but I'm missing it...I need to search Active Directory using a first name and last name and display all users who match in a listbox. Can someone point me in the right direction, or if someone has already asked the same question link me to it? Thanks in advance!

推荐答案

请尝试以下操作:-

DirectorySearcher d = new DirectorySearcher(somevalue);    
d.Filter = string.Format("(&(objectCategory=person)(objectClass=user)(givenname={0})(sn={1}))", firstname, lastname);

也来自如何使用C#在Active Directory中搜索用户

//Create a shortcut to the appropriate Windows domain
PrincipalContext domainContext = new PrincipalContext(ContextType.Domain,
                                                      "myDomain");

//Create a "user object" in the context
using(UserPrincipal user = new UserPrincipal(domainContext))
{
 //Specify the search parameters
 user.Name = "he*";

 //Create the searcher
 //pass (our) user object
 using(PrincipalSearcher pS = new PrincipalSearcher())
 {
  pS.QueryFilter = user;

  //Perform the search
  using(PrincipalSearchResult<Principal> results = pS.FindAll())
  {
   //If necessary, request more details
   Principal pc = results.ToList()[0];
   DirectoryEntry de = (DirectoryEntry)pc.GetUnderlyingObject();
  }
 }
} 
//Output first result of the test
MessageBox.Show(de.Properties["mail"].Value.ToString());

这篇关于C#在AD中搜索用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆