Solved

Retrieve Data from Active Directory...

Posted on 2007-03-19
17
1,066 Views
Last Modified: 2013-12-24
Hi,
I populat DataSet with 2 columns by retrieve data from AD.
The first column is CN (commonName) and the second is a property of the CN (some id).
What i get at the Grid is : |  CN         |       ID                    |
                                        | ----------------------------------|
                                        |  100        |   System.Object[]  |

I wrote in order to get it :      string sql = @"SELECT CN, ID  FROM 'LDAP://Domain'   WHERE CN =100'";
I understand that when i called the ID field , i actually called an object, but now, how can i get to the properties of that object (The ID value)??
Thanks
0
Comment
Question by:udir
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 8
17 Comments
 
LVL 3

Expert Comment

by:Kate12
ID: 18753006
Have you consider using System.DirectoryServices?

using (DirectoryEntry entry = new DirectoryEntry("LDAP://DomainName"))
            {
                foreach (string propertyName in entry.Properties.PropertyNames)
                {
                    System.Object value = entry.Properties[propertyName].Value;
                    if (value != null)
                    {
                        Console.WriteLine(value);
                    }
                    // If it is multiple values
                    if (value is IEnumerator)
                    {
                        IEnumerator enumerator = (IEnumerator)value;
                        while (enumerator.MoveNext())
                        {
                            Console.WriteLine(enumerator.Current.ToString());
                        }
                    }
                }
            }
0
 
LVL 1

Author Comment

by:udir
ID: 18754467
Hi,
Thanks for the reply.
I noticed that the values that i need did't get into the results.
I will give an example of how the AD looks like and what i need to retrieve from it :
DC = internet, DC=Test  ----->
                                   OU = CityName  ----->
                                             CN = 1000  -----> (CN is a property of the OU above which has a value = 10)
                                                       ID = 10    (ID is a property of the CN above which has a value = 10)
1) I need to get all the values of a property of the OU, which is the CN values = 1000 and so on.
2) also all the values of a property of the CN, which is the ID values = (10 and so on).
How can i get it with the Foreach statment?
Thanks
                                           
0
 
LVL 3

Expert Comment

by:Kate12
ID: 18754680
I believe you need to use the DirectorySearcher instead... :

DirectoryEntry entry = new DirectoryEntry("LDAP://....");
DirectorySearcher searcher = new DirectorySearcher(entry);
searcher.Filter = "(&(objectClass=ou)(CN=100)(ID=10))";
foreach (SearchResult result in searcher.FindAll())
{
    // Properties for the Entry Result.
    //result.Properties
    // Or Get the Directory For this result.
    DirectoryEntry entryResult = result.GetDirectoryEntry();
}

If you need to tweek the syntax you can have look at http://msdn2.microsoft.com/en-us/library/aa746475.aspx for further info.

HTH
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:udir
ID: 18755078
Hi,
Kate12, I'm desperate   (:   or   :(
i just can't get the values.
I wrote :
------------------------------------------------------------
        DirectoryEntry root = new DirectoryEntry(
          "LDAP://10.1.1.1/DC=Internet,DC=Test",
          "User","Password", AuthenticationTypes.ServerBind);

        DirectorySearcher searcher = new DirectorySearcher(root);
        searcher.Filter = "(&(objectClass=user))";
        SearchResultCollection results;
        results = searcher.FindAll();
               
            foreach (SearchResult result in results)
            {
                ResultPropertyCollection rpc = result.Properties;
                foreach (string key in rpc.PropertyNames)
                {
                    foreach (object values in rpc[key])
                    {
                        switch (key)
                        {
                            case "CN":
                                Console.WriteLine("Value is : {0}" + values);
                                break;
                            case "ID":
                                Console.WriteLine("Value is : {0}" + values);
                                break;
                        }
                    }
                }
            }
But it dosn't work (what i'm missing?????)
pls, can you help with that?????
Thanks in advance  
0
 
LVL 3

Expert Comment

by:Kate12
ID: 18755162
Did you debug this code? What happens when you put a breakpoint at the swich statement? Can you inspect the rpc object and see if there is anythign there? ensure that the 'key' matches with the case statements i think that the 'CN' key may be bound as 'cn'.

Properties at AD are noted as single valued or multi-value,  so a property value is always a collection where when it is a single-valued property only the zero index of the property.Value is populated.

HTH
0
 
LVL 1

Author Comment

by:udir
ID: 18755212
Yes sure i debbuged it.
The values i get are on the CN level , and my problem is that i don't know how to " drill down "
one more level to the CN properties and get their values (which one of them is - ID property)
Any Idea? (maybe another Foreach statment inside the switch statment?)
0
 
LVL 3

Accepted Solution

by:
Kate12 earned 500 total points
ID: 18755254
Have you looked via adsiviewer or any other Adsi browser to see if there is anything under the CN on that particular Entry? What you are doing seems to be correct about now... you should be getting your values.
A friend has written a good article on this too, have a look at http://www.codeproject.com/dotnet/QueryADwithDotNet.asp.

HTH
0
 
LVL 1

Author Comment

by:udir
ID: 18755316
Oh, i think the problem is not as i mantioned:
In the loop i get all the values!!!
So now, i only need to filtered them, in order to get only the values of the ID property!!!
How can i do this??
0
 
LVL 3

Expert Comment

by:Kate12
ID: 18755330
as i mentioned before in the code you got to expande your query:

searcher.Filter = "(&(objectClass=user)(ID=10))";

Also in order to optmize the query you can use the PropertyToLoad property at the searcher:
searcher.PropertiesToLoad.Add("cn");
searcher.PropertiesToLoad.Add("id");

HTH
0
 
LVL 1

Author Comment

by:udir
ID: 18755379
OK, let me sumerize my problem for now :
as i wrote above - i do get all the values.
now, i need to get only the values of the ID property (which is a property of CN).
The problem is that in the switch statment the propertyNames refers only to the OU propertyNames
and not to the CN propertyNames and thats why i don't get any values of the ID.
0
 
LVL 3

Expert Comment

by:Kate12
ID: 18755445
I dont understand. the property 'cn' of a DirectoryEntry (the one you are using on your switch statement) is short for "Common Name" like "John Doe". In order to get the OU properties you have to use a different LDAP path pointing to your OU e than re-write the query accordingly.
0
 
LVL 1

Author Comment

by:udir
ID: 18755446
I also added
searcher.PropertiesToLoad.Add("cn");
searcher.PropertiesToLoad.Add("id");
and
searcher.Filter = "(&(objectClass=user))";
but the switch statment get only the values of the CN properties but not the values of the ID property
0
 
LVL 3

Expert Comment

by:Kate12
ID: 18755452
Are you using AdsiViewer?
0
 
LVL 1

Author Comment

by:udir
ID: 18755459
OK, lets leave the cn (because i succeed to get the values).
how do i get the ID values?
0
 
LVL 3

Expert Comment

by:Kate12
ID: 18755460
AdsiViewer will allow you to browse your LDAP provider so you can verify the appropiate path and also will show you the property values (if any) and will also point out if they are single-valued or multi-valued.
0
 
LVL 1

Author Comment

by:udir
ID: 18755465
i'm using ADSI Edit
0
 
LVL 1

Author Comment

by:udir
ID: 18755515
and the tree is like i wrote before:
DC = internet, DC=Test  ----->
                                   OU = CityName  ----->
                                             CN = 1000  -----> (CN is a property of the OU above which has a value = 10)
                                                       ID = 10    (ID is a property of the CN above which has a value = 10)
so i need all the ID's values.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Let's recap what we learned from yesterday's Skyport Systems webinar.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question