Searching the properties in the Active Directory
-
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
-
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
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")
-
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")
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