I'm currently making a request in the active directory, our database user ID is the same as the ID of the active directory.
I pass the user ID along with the domain and the path to get what I need. My desire is to get the manager email address from the passed user ID. What I return when I get the manager property is the distinguished name.
Search for a user manager entry in Active Directory
This post is my exact problem, but this is an old article, and there are no additional descriptions on how to move forward, and the OP knew what to do next with a distinguished name. True, I do not.
So my question is: how do I get the property of the email address from the name that I still saved as a string with the LDAP prefix: // + "MyDistinguishedName"?
public string GetManagerEmail(string ActiveDirectoryPath, string ActiveDirectoryDomain, bool email) { DirectoryEntry entry = new DirectoryEntry(ActiveDirectoryPath); try { DirectorySearcher search = new DirectorySearcher(entry); search.Filter = "(SAMAccountName=" + workerID + ")"; search.PropertiesToLoad.Add("cn"); search.PropertiesToLoad.Add("givenname"); //firstname search.PropertiesToLoad.Add("sn");//surname search.PropertiesToLoad.Add("manager"); search.PropertiesToLoad.Add("email"); SearchResult result = search.FindOne(); if (null == result) { return workerID; } if (email) { return (string)result.Properties["email"][0]; } else { return (string)result.Properties["manager"][0]; //return (string)result.Properties["manager"].IndexOf[]; } } catch (Exception ex) { throw new Exception("Error. " + ex.Message); } finally { entry.Close(); } }
The above is the method that I use to get the data I need. Any input or improvements will be appreciated.
thanks
THIS IS MY DECISION FOR THOSE WHICH MAY BE INTERESTED
string domainAndUsername = ActiveDirectoryDomain + @"\" + workerID; DirectoryEntry manager = new DirectoryEntry(ActiveDirectoryPath); try { if (manager != null) {
source share