C# adc中的属性搜索#
我目前可以通过NT ID搜索广告,使用:C# adc中的属性搜索#,c#,active-directory,C#,Active Directory,我目前可以通过NT ID搜索广告,使用: PrinciplayContext domain = new PrincipalContext(ContextType.Domain) UserPrincipal byName = UserPrincipal.FindByIdentity(domain, txtTest.Text.Trim()); 从那里我可以找到任何和所有的扩展容易 我想做的是能够使用employeeID属性进行拉取,并获取sn、SAMAccountName、employeeID 我
PrinciplayContext domain = new PrincipalContext(ContextType.Domain)
UserPrincipal byName = UserPrincipal.FindByIdentity(domain, txtTest.Text.Trim());
从那里我可以找到任何和所有的扩展容易
我想做的是能够使用employeeID属性进行拉取,并获取sn、SAMAccountName、employeeID
我想知道是否有一种方法可以创建一个新的UserPrincipal并以这种方式进行搜索。
例子:
UserPrincipal.findbyemplyeid
我的想法是:
public static new UserPrincipal FindByEmployeeID(PrincipalContext context, string identityValue)
{
//dont know what to do here... sorry noob on this
}
例如:
当我使用:
try
{
PrincipalContext AD = new PrincipalContext(ContextType.Domain);
UserPrincipal byEmployeeID = UserPrincipal.FindByIdentity(AD, "(&(objectCategory=person)(objectclass=user)(employeID=JR02251206))");
MessageBox.Show(byEmployeeID.GetEmail());
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
我有一个例外:
对象引用未设置为对象的实例。我试过只使用just(employeeID=JR02251206)或(objectclass=user)(employeeID=JR02251206)的原则,但得到了相同的错误 解决方案:
try
{
DirectoryEntry enTry = new DirectoryEntry("LDAP://OU=Users,OU=Users and Groups,DC=BLAH,DC=com");
DirectorySearcher mySearcher = new DirectorySearcher(enTry);
mySearcher.Filter = "(&(objectClass=user)(extensionAttribute13=" + txtTest.Text.Trim() + "))";
SearchResult result = mySearcher.FindOne();
txtBlob.Text = result.Properties["displayname"][0].ToString() +"\n";
txtBlob.Text += result.Properties["mail"][0].ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
试试这个:
PrinciplayContext AD= new PrincipalContext(ContextType.Domain)
UserPrincipal searchTemplate = new UserPrincipal(AD);
searchTemplate.EmployeeId = tempUserId;
PrincipalSearcher ps = new PrincipalSearcher(searchTemplate);
UserPrincipal user = (UserPrincipal) ps.FindOne();
这应该和你开始的差不多了
凯文·奥克特