Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 627
Simple Code Avatar asked Jan 27 '26 04:01

Simple Code


2 Answers

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

like image 134
Ferus7 Avatar answered Jan 28 '26 16:01

Ferus7


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;

        }
    }
}
like image 22
Simple Code Avatar answered Jan 28 '26 18:01

Simple Code



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!