PrincipalContext - I Can't connect to a local

2019-05-09 11:03发布

问题:

I have installed an openldap for windows server and I use LDAPAdmin to connect to it by its default values :

Server=ldap://localhost:389
Base:dc=maxcrc,dc=com
UserName:cn=Manager,dc=maxcrc,dc=com
Password:secret

Now I want to use PrincipalContext to add users to my ou=People The problem is I can't even connect to the server using PrincipalContext. I have searched the Net and found a lot of answers on how to connect to a ldap server but none of them are working for me.I keep getting a NullReferenceException or ServerNotFoundException for different combination of provided parameters for PrincipalContext constructor. I thought I could use PrincipalContext as one of the follows :

new PirncipalContext(ContextType.Domain,"maxcrc.com","dc=maxcrc,dc=com")

or

new PrincipalContext(ContextType.ApplicationDirectory,"localhost:389","dc=maxcrc,dc=com")

but none of them are working.Some say that I should provide username and password so I did that but I keep getting exception.

So please tell me how can I use PrincipalContext to connect to openldap ?

P.S. My computer is already joined to an Active Directory domain controller.

回答1:

As the official documentation on MSDN states:

The System.DirectoryServices.AccountManagement namespace provides uniform access and manipulation of user, computer, and group security principals across the multiple principal stores: Active Directory Domain Services (AD DS), Active Directory Lightweight Directory Services (AD LDS), and Machine SAM (MSAM). System.DirectoryServices.AccountManagement manages directory objects independent of the System.DirectoryServices namespace.

Those classes are useful for Active Directory only - they are not portable to other, "generic" LDAP stores.

If you need to support OpenLDAP, either use DirectoryEntry and those functions, or use the lower level System.DirectoryServices.Protocol (bare-bones LDAP) layer.