在Active目录中,我正在尝试连接到 LDAP
服务器并尝试检查 user
我们是否有效 . 但是下面的代码抛出了异常错误 . 我不知道我哪里出错了 . 即使连接是否正确 . 我尝试了相同的连接名称和密码到 Apache directory studio
,我能够成功连接但不通过代码 . 它从 myLdapConnection
抛出异常,请你检查我哪里出错了 . 我在 Apache directory studio
中使用的连接是安全连接..我是否需要指定 LDAPS
或ldap才能工作
public void GetAllADUsers( string username)
{
try
{
DirectoryEntry myLdapConnection = new DirectoryEntry();
myLdapConnection.Path = "LDAP://server:port/o=value";
myLdapConnection.Username = @"CN=cn,OU=ou,OU=Users,O=o";
myLdapConnection.Password = "password";
//myLdapConnection.AuthenticationType = AuthenticationTypes.Secure;
DirectorySearcher search = new DirectorySearcher(myLdapConnection) { Filter = ( "(&(objectClass=user)") };
SearchResultCollection allResults = search.FindAll();
if(allResults.Count == 0)
{
Console.WriteLine("not found");
}
else
{
Console.WriteLine ("found");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
Note 用户名,如果我指定 = @"CN=cn,OU=ou,OU=Users,O=o";
myLdapConnection抛出DirectorySearcherCOMException,但如果只传递用户名= cn 表示cn值..这给出了所有值
例外
在System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)\ r \ n at System.DirectoryServices.DirectoryEntry.Bind()\ r \ n,位于System.DirectoryServices.DirectoryEntry.get_AdsObject()\ r \ n,位于System.DirectoryServices.DirectorySearcher .Andault.GetAllADUsers(String username