private void ADQuery()
{
try
{
string connString = "LDAP://10.0.10.11/dc=abcd,dc=ac,dc=in";//demo url
string username = "cn=admin,dc=abcd,dc=ac,dc=in";
string name = "cn=admin,dc=abcd,dc=ac,dc=in";
string password = "secrate";
string name2 = "10.0.10.11admin";// domainusername
DirectoryEntry de = new DirectoryEntry(connString, name2, "secrate", AuthenticationTypes.Secure);
var search = new DirectorySearcher(de)
{
Filter = "(&(ou=employee)(objectClass=inetOrgPerson))"
};
search.SearchScope = SearchScope.Subtree;
// error thrown by this statement
SearchResult results = search.FindOne();
if (results != null)
{
string email = results.Properties["mail"].ToString();
}
}
catch (Exception ex)
{
throw ex;
}
}
我得到这个错误:
"/"应用程序中的服务器错误。
发生本地错误。
描述:在执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误以及错误在代码中的来源的更多信息。
异常详细信息:
System.DirectoryServices.DirectoryServicesCOM异常:发生本地错误
查看您的代码,唯一让我感到突出的问题是您的过滤器:
Filter = "(&(ou=employee)(objectClass=inetOrgPerson))"
在Active Directory中,inetOrgPerson
类不用于用户。如果你想找到用户对象,你应该使用这个作为过滤器:
Filter = "(&(objectClass=user)(objectCategory=person))"
您也无法像尝试的那样在ou
上进行筛选。若要将搜索限制为特定的OU,请在用于SearchRoot
的DirectoryEntry
对象的LDAP字符串中使用该OU。在您的情况下,它是您的connString
变量:
string connString = "LDAP://10.0.10.11/OU=employee,dc=abcd,dc=ac,dc=in";
此外,AuthenticationTypes.Secure
是默认的AuthenticationType,因此您不需要在构造函数中指定它:
DirectoryEntry de = new DirectoryEntry(connString, name2, "secrate", AuthenticationTypes.Secure);
同样,Subtree
是默认的SearchScope,所以您不需要这行:
search.SearchScope = SearchScope.Subtree;