Resolved: Passing the servername/Ldap inside “DirectoryEntry” Vs “PrincipalContext”

In this post, we will see how to resolve Passing the servername/Ldap inside “DirectoryEntry” Vs “PrincipalContext”


I have this action method inside my ASP.NET MVC-5 .net 4.6:-
so after around one day of testing i realize that for the DirectoryEntry I need to pass the server/ldap as follow ("LDAP://,DC=com", ADusername, ADpassword)) , while for the PrincipalContext we need to pass it as follow:- (ContextType.Domain, "", ADusername, ADpassword)).. so i can not pass the ldap inside the PrincipalContext nor the servrname only inside the DirectoryEntry .. so is this the case? or i am doing things wrongly ? Thanks

Best Answer:

You are correct.
The System.DirectoryServices.AccountManagement namespace (PrincipalContext, UserPrincipal, etc.) was created to simplify things. However, it just uses the System.DirectoryServices namespace (DirectoryEntry, etc.) in the background. (except for ValidateCredentials, which uses System.DirectoryServices.Protocols.LdapConnection).
I prefer to always use DirectoryEntry and friends because it gives me more control over performance. That’s something I wrote an article about: Active Directory: Better performance

If you have better answer, please add a comment about this, thank you!