locked
Trying to get complete information of user in a group RRS feed

  • Question

  • User42671626 posted

    Hi,

    I have some problems with getting the information about the users in a group. I need to get the Guid of the user and his name. I am using Win2k3Ent.Server and Framework 1.x. Thanks for you help.

                                                                        Radek

    Here is my code:

    SearchResultCollection resultCollection1;

    DirectoryEntry ent = new DirectoryEntry("LDAP://bla.bla.bla/CN=Staff,CN=Users,DC=bla,DC=bla,DC=bla");

    DirectorySearcher directorySearcher = new DirectorySearcher(ent);

    directorySearcher.Filter = "(& (objectClass=user)(objectCategory=person)(memberOf=CN=Staff))

    SortedList users = new SortedList();

    resultCollection1 = directorySearcher.FindAll();

    foreach(SearchResult resEnt in resultCollection1)
    {
       try
       {
           DirectoryEntry ent = resEnt.GetDirectoryEntry();

           users.Add(ent.NativeGuid.ToString(), ent.Properties["GivenName"].Value.ToString());
       }
    }

    Wednesday, March 15, 2006 8:30 AM

All replies

  • User1540266618 posted

    Hi patrik

    As far as I know, you don't have to use the GetDirectoryEntry() on your search result. And, if you tell the DirectorySearch which properties to return, you can get all the info you need.

    Here's a simple exampel (not sure if it works as you want with the SortedList):

    //setup the searching entries
    DirectoryEntry deParent = new DirectoryEntry(LDAP://bla.bla.bla/CN=Staff,CN=Users,DC=bla,DC=bla,DC=bla
    );
    //deParent.Username = Config.Settings.UserName;
    //deParent.Password = Config.Settings.Password;
    deParent.AuthenticationType = AuthenticationTypes.Secure;

    string filter = "(&(objectClass=user)(objectCategory=person)(memberOf=CN=Staff))"
    ;

    string[] properties = new string[] { "distinguishedName", "sAMAccountName", "givenName", "sn", "telephoneNumber", "mobile", "mail", "department"
    };

    DirectorySearcher ds =
    new
    DirectorySearcher(deParent, filter, properties);
    ds.PageSize = 1000;

    using
    (deParent) {
       SortedList users =
    new
    SortedList();

       
    using
    (SearchResultCollection src = ds.FindAll()) {
          
    foreach(SearchResult sr in
    src) {
             
    foreach(string prop in
    properties) {
                
    if
    (sr.Properties.Contains(prop)) {
                   users.add(sr.Properties[prop][0].toString());
                }
             }
          }
       }
    }

    If you need the guid, look in at this poste, dunnry has a solution for that posted there: http://forums.asp.net/1217077/ShowPost.aspx

    Also, the same exampel as the one above, the main difference is that the results are saved in a DataSet: http://forums.asp.net/900432/ShowPost.aspx (scroll down to dunnry's post again).

    Hope this helps, Egil.

    Thursday, March 16, 2006 4:59 AM
  • User1354132231 posted
    One additional comment on top of Egil's:  If you do not supply any attributes to load to the DirectorySearcher, it will return all populated and non-constructed attributes.

    Constructed attributes are calculated by the directory and are not actually stored (e.g. 'tokenGroups').
    Thursday, March 16, 2006 9:07 AM
  • User42671626 posted
    Thanks for your help, boys. Its OK now.
    Thursday, March 16, 2006 4:20 PM