Simple Code
Simple Code

Reputation: 2594

Connecting to LDAP server throws NullReferenceException

I am trying to connect to the online test LDAP server specified here using System.DirectoryServices.AccountManagement like this:

try
{
    using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "ldap.forumsys.com:389", "dc=example,dc=com", "cn=read-only-admin,dc=example,dc=com", "password"))
    {
         using (var searcher = new PrincipalSearcher(new UserPrincipal(ctx )))
         {
              foreach (var result in searcher.FindAll().Take(usersCount))
              {
                 DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry;
              }
        }
    }
}
catch(Exception ex)
{
    Console.WriteLine(ex.Message);
}

But it throws the following exception:

Object reference not set to an instance of an object.

Could you please tell what is wrong with my code and how to be able to connect to that LDAP server?

PS: I am able to connect to that server using Apache Directory Studio

Stack Trace :

at System.DirectoryServices.AccountManagement.PrincipalContext.ReadServerConfig(String serverName, ServerProperties& properties) at System.DirectoryServices.AccountManagement.PrincipalContext.DoServerVerifyAndPropRetrieval() at System.DirectoryServices.AccountManagement.PrincipalContext..ctor(ContextType contextType, String name, String container, ContextOptions options, String userName, String password) at System.DirectoryServices.AccountManagement.PrincipalContext..ctor(ContextType contextType, String name, String container, String userName, String password) at ConsoleApp1.Program.GetGroups(String userName) in C:\Users\Simple Code\source\repos\ConsoleApp1\ConsoleApp1\Program.cs:line 48

Upvotes: 3

Views: 1502

Answers (2)

Simple Code
Simple Code

Reputation: 2594

Using DirectoryEntry it works for me as following:

using (var searcher = new DirectorySearcher(new DirectoryEntry("LDAP://ldap.forumsys.com:389/dc=example,dc=com", "", "", AuthenticationTypes.None)))
{
    searcher.Filter = "((objectClass=person))";
    searcher.PropertiesToLoad.Add("mail");//email
    searcher.PropertiesToLoad.Add("givenName");//first name
    searcher.PropertiesToLoad.Add("sn"); //last name
    searcher.PropertiesToLoad.Add("telephoneNumber");
    searcher.PropertiesToLoad.Add("description");
    searcher.PropertiesToLoad.Add("memberOf"); // groups

    var activeDirectoryStaffs = searcher.FindAll();
    if (activeDirectoryStaffs != null)
    {
        for (int i = 0; i < activeDirectoryStaffs.Count; i++)
        {
            SearchResult result = activeDirectoryStaffs[i];
            var Email = result.Properties.Contains("mail") ? (string)result.Properties["mail"][0]:null;
            var Mobile = result.Properties.Contains("telephoneNumber") ? (string)result.Properties["telephoneNumber"][0] : null;
            var FirstName = result.Properties.Contains("givenName") ? (string)result.Properties["givenName"][0] : null;
            var LastName = result.Properties.Contains("sn") ? (string)result.Properties["sn"][0] : null;
            var Description = result.Properties.Contains("description") ? (string)result.Properties["description"][0] : null;

        }
    }
}

Upvotes: 3

Ferus7
Ferus7

Reputation: 727

As said here, the problem could be that you try to connect to an Apache Directory Studio with the class PrincipalContext that not supports this OpenLDAP,

so one way to go is using the DirectoryEntry class

Upvotes: 4

Related Questions