Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Searching the properties in the Active Directory

Searching the properties in the Active Directory

Scheduled Pinned Locked Moved C#
helpwindows-adminalgorithmsdata-structuresquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    khuzwayom
    wrote on last edited by
    #1

    Happy New Year Everyone! Please help I am writing a code that searches the Active directory, it first get the list of properties, then use each property name to get its value. I filter the directory searcher by the cn. My problem is that for some people the search doesn't return the "mail" property, this is the property I need most. Please find my code snippet below. DirectoryEntry de = new DirectoryEntry("LDAP://" + "AFLIFEDC04", @"aflife\alsql", "perseus", AuthenticationTypes.FastBind); string[] propertiesFound; string Email = ""; DirectorySearcher ds = new DirectorySearcher(de); //Use the users full name to get the email address from directory services ds.Filter = "(cn=" + FullName + ")"; ds.SearchScope = SearchScope.Subtree; try { SearchResultCollection results = ds.FindAll(); foreach (SearchResult result in results) { int a = 0; ResultPropertyCollection propc = result.Properties; DirectoryEntry dey = result.GetDirectoryEntry(); propertiesFound = new string[propc.Count]; // Loading all the properties return into the propertieFound array foreach (string propertyName in propc.PropertyNames) { //This where the problem is, for some I get like 38 properties, //for others I get 34. What is the cause of and what is the //solution to it. propertiesFound[a] = propertyName; a++; } } } Thank you in advanced for your solutions. Kind Regards Mpumelelo Khuzwayo:confused:

    khuzym

    J 1 Reply Last reply
    0
    • K khuzwayom

      Happy New Year Everyone! Please help I am writing a code that searches the Active directory, it first get the list of properties, then use each property name to get its value. I filter the directory searcher by the cn. My problem is that for some people the search doesn't return the "mail" property, this is the property I need most. Please find my code snippet below. DirectoryEntry de = new DirectoryEntry("LDAP://" + "AFLIFEDC04", @"aflife\alsql", "perseus", AuthenticationTypes.FastBind); string[] propertiesFound; string Email = ""; DirectorySearcher ds = new DirectorySearcher(de); //Use the users full name to get the email address from directory services ds.Filter = "(cn=" + FullName + ")"; ds.SearchScope = SearchScope.Subtree; try { SearchResultCollection results = ds.FindAll(); foreach (SearchResult result in results) { int a = 0; ResultPropertyCollection propc = result.Properties; DirectoryEntry dey = result.GetDirectoryEntry(); propertiesFound = new string[propc.Count]; // Loading all the properties return into the propertieFound array foreach (string propertyName in propc.PropertyNames) { //This where the problem is, for some I get like 38 properties, //for others I get 34. What is the cause of and what is the //solution to it. propertiesFound[a] = propertyName; a++; } } } Thank you in advanced for your solutions. Kind Regards Mpumelelo Khuzwayo:confused:

      khuzym

      J Offline
      J Offline
      Justin Perez
      wrote on last edited by
      #2

      I ran into this problem a while back, but I don't remember what the problem was, and why you can't return the E-Mail property the way you do other properties. Anyway, here is the code I came up with the return the E-Mail property.

      public static string getUserEMail(string fullname)
      {
        string email = string.Empty;
      
        DirectorySearcher search = new DirectorySearcher();
        search.Filter = "(cn=" + fullname + ")";
        search.PropertiesToLoad.Add("mail");
      
        try
        {
          SearchResult result = search.FindOne();
          if (null != result)
          {
            email = (String)result.Properties\["mail"\]\[0\];
            return email;
          }
          else
          {
            return "Could not find Email address!";
          }
        }
        catch (Exception ex)
        {
          return ex.Message;
        }
      }
      

      "If an Indian asked a programming question in the forest, would it still be urgent?" - John Simmons / outlaw programmer I get all the news I need from the weather report - Paul Simon (from "The Only Living Boy in New York")

      K 1 Reply Last reply
      0
      • J Justin Perez

        I ran into this problem a while back, but I don't remember what the problem was, and why you can't return the E-Mail property the way you do other properties. Anyway, here is the code I came up with the return the E-Mail property.

        public static string getUserEMail(string fullname)
        {
          string email = string.Empty;
        
          DirectorySearcher search = new DirectorySearcher();
          search.Filter = "(cn=" + fullname + ")";
          search.PropertiesToLoad.Add("mail");
        
          try
          {
            SearchResult result = search.FindOne();
            if (null != result)
            {
              email = (String)result.Properties\["mail"\]\[0\];
              return email;
            }
            else
            {
              return "Could not find Email address!";
            }
          }
          catch (Exception ex)
          {
            return ex.Message;
          }
        }
        

        "If an Indian asked a programming question in the forest, would it still be urgent?" - John Simmons / outlaw programmer I get all the news I need from the weather report - Paul Simon (from "The Only Living Boy in New York")

        K Offline
        K Offline
        khuzwayom
        wrote on last edited by
        #3

        Good Morning Justin Thank you for your reply, I have tried implementing it, the problem it returns the adspath property and not mail property. I don't know if you hav an idea what is the cause. Thank you again. I will keep on trying different things, I have a feeling the problem is very small!

        khuzym

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups