C#在广告中搜索用户
我天生不是程序员,所以我提前道歉。我到处搜寻,找到了做一件事的10种不同方法的点点滴滴。我试图做的似乎很简单,但我没有做到……我需要使用名字和姓氏搜索Active Directory,并在列表框中显示匹配的所有用户。有人能给我指出正确的方向吗?或者如果有人已经问过同样的问题,请将我与之联系起来?提前谢谢 试试这样的方法:-C#在广告中搜索用户,c#,active-directory,C#,Active Directory,我天生不是程序员,所以我提前道歉。我到处搜寻,找到了做一件事的10种不同方法的点点滴滴。我试图做的似乎很简单,但我没有做到……我需要使用名字和姓氏搜索Active Directory,并在列表框中显示匹配的所有用户。有人能给我指出正确的方向吗?或者如果有人已经问过同样的问题,请将我与之联系起来?提前谢谢 试试这样的方法:- DirectorySearcher d = new DirectorySearcher(somevalue); d.Filter = string.Format("(
DirectorySearcher d = new DirectorySearcher(somevalue);
d.Filter = string.Format("(&(objectCategory=person)(objectClass=user)(givenname={0})(sn={1}))", firstname, lastname);
也来自
//创建相应Windows域的快捷方式
PrincipalContext domainContext=新PrincipalContext(ContextType.Domain,
“myDomain”);
//在上下文中创建“用户对象”
使用(UserPrincipal用户=新的UserPrincipal(domainContext))
{
//指定搜索参数
user.Name=“he*”;
//创建搜索者
//传递(我们的)用户对象
使用(PrincipalSearcher pS=new PrincipalSearcher())
{
pS.QueryFilter=用户;
//执行搜索
使用(PrincipalSearchResult results=pS.FindAll())
{
//如有必要,请索取更多详细信息
Principal pc=results.ToList()[0];
DirectoryEntry de=(DirectoryEntry)pc.GetUnderlineingObject();
}
}
}
//输出测试的第一个结果
Show(de.Properties[“mail”].Value.ToString());
当然,发布后不久,我找到了答案:)
以下是链接:
//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());
// create your domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "servername","username","password");
UserPrincipal qbeUser = new UserPrincipal(ctx);
qbeUser.GivenName = "fname";
qbeUser.Surname = "lname";
// qbeUser.DisplayName= "fname lname";
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);
// find all matches
foreach (var found in srch.FindAll())
{
lstUser.Items.Add(found.ToString());
}