Link to home
Start Free TrialLog in
Avatar of Mahesh Yadav
Mahesh YadavFlag for India

asked on

Ldapv3 not returning fields which are empty in AD profile

I am trying to run the following code it is working fine but there is an issue.
the issue is it is only those fields which has a value.
All those fields which are empty are not coming.

for example, if in my user in AD has a value in email field then it is coming otherwise not.
I am also giving the code, could any one help me out here.

I am using LDapv3 here


here is the code.
-------------------------------------

  public static void Main(string[] args)
        {
            try
            {
               
               

                //LdapDirectoryIdentifier ldapDir = new LdapDirectoryIdentifier("CBMCC-A8-HPC06", 389);

                LdapDirectoryIdentifier ldapDir = new LdapDirectoryIdentifier("192.168.2.170", 389);
                LdapConnection ldapConn = new LdapConnection(ldapDir);
                //You may need to try different types of Authentication depending on your setup
                ldapConn.AuthType = AuthType.Basic;
                //Update the next line to include the Fully Qualified LDAP name
                // of the user along with that user's password
                System.Net.NetworkCredential myCredentials =
                    new System.Net.NetworkCredential("jet\\administrator", "1nT1meTec");
                ldapConn.SessionOptions.ProtocolVersion = 3;
                //This is the actual Connection establishment here
                ldapConn.Bind(myCredentials);

                Console.WriteLine("LdapConnection is created successfully.");

               

                //Search
                //SearchRequest findme = new SearchRequest("dc=jet,dc=in", "(objectClass=printQueue)", System.DirectoryServices.Protocols.SearchScope.Subtree,"*", "+");

                SearchRequest findme = new SearchRequest("CN=Users;dc=Jet,dc=in", "(objectClass=user)",
                    System.DirectoryServices.Protocols.SearchScope.Subtree, "*", "+");

                //findme.DistinguishedName = "dc=jet,dc=in"; //Find all People in this ou
                //findme.Filter = "(objectClass=user)"; //The type of entry we are looking for
                //findme.Scope = System.DirectoryServices.Protocols.SearchScope.Subtree; //We want all
                //entries below this ou

                SearchResponse results = (SearchResponse)ldapConn.SendRequest(findme); //Run the query

                //and get results
                SearchResultEntryCollection entries = results.Entries;
                List<string> lstAtributes = new List<string>();
                if (entries != null && entries.Count > 0) {
                    SearchResultEntry entry = entries[0];
                    IDictionaryEnumerator attributes = entry.Attributes.GetEnumerator();
                    while (attributes.MoveNext()) {
                        lstAtributes.Add(((DirectoryAttribute)attributes.Value).Name);
                    }
                }
                for (int ii = 0; ii < entries.Count; ii++)//Iterate through the results
                {
                    SearchResultEntry entry = entries[ii];
                    IDictionaryEnumerator attribEnum = entry.Attributes.GetEnumerator();
                    Console.WriteLine("==================================================================");
                    while (attribEnum.MoveNext())//Iterate through the result attributes
                    {
                        //Attributes have one or more values so we iterate through all the values
                        //for each attribute
                        DirectoryAttribute subAttrib = (DirectoryAttribute)attribEnum.Value;
                     
                        for (int ic = 0; ic < subAttrib.Count; ic++)
                        {
                            //Attribute Name below
                            Console.Write(subAttrib.Name + ": ");
                            lstAtributes.Add(subAttrib.Name);
                            //Attribute Sub Value below
                            Console.WriteLine(subAttrib[ic].ToString());
                        }

                    }
                }
               
            }
            catch (Exception e)
            {
                Console.WriteLine("\r\nUnexpected exception occured:\r\n\t" + e.GetType() + ":" + e.Message);
            }
            finally
            {
                Console.ReadLine();
            }
        }
Avatar of markpalinux
markpalinux
Flag of United States of America image

In your code are you telling it what attributes you are looking for?

Here is VB Script , but I would think the same applies
http://www.rlmueller.net/ADOSearchTips.htm

Give a base, filter, and list of attributes you want.

If you say find fred, you get the user attributes for fred (only populated ones )
however if you say find fred tell me samaccountname,mail,sid then you should get those attributes back if one is null.

Mark
Avatar of Mahesh Yadav

ASKER

this is my mistake, actually all I need to get all attribute from Ad or any other directory service which ldapv3 is able to connect no matter whether those attributes has a value in them.

My need is to get those attributes and show them in a list to the user.
ASKER CERTIFIED SOLUTION
Avatar of markpalinux
markpalinux
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial