Request Active Directory to get the distinguished name email property directly?

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) { // get e-mail of manager if (manager.Properties["mail"] != null && manager.Properties["mail"].Count > 0) { string managersEMail = manager.Properties["mail"].Value.ToString(); return managersEMail; } } //No email available, use contract manager return string.Empty; } catch (Exception ex) { throw new Exception("Error. " + ex.Message); } finally { manager.Close(); } 
+6
source share
1 answer

There is no β€œmagic” shortcut for receiving an email manager.

After you have received the DN (distinguished name) of your manager (in a string variable named managerDN ), you need to bind to Active Directory again, creating another instance of DirectoryEntry to capture the manager information user.

Try something like this:

  .....(your other code up here)...... else { string managerDN = result.Properties["manager"][0].ToString(); // fully-qualified DN for manager string managerFQDN = "LDAP://" + managerDN; DirectoryEntry manager = new DirectoryEntry(managerFQDN); if(manager != null) { // get e-mail of manager if(manager.Properties["mail"] != null && manager.Properties["mail"].Count > 0) { string managersEMail = manager.Properties["mail"].Value.ToString(); return managersEMail; } } // we couldn't retrieve the manager e-mail return string.Empty; } 
+8
source

Source: https://habr.com/ru/post/922553/


All Articles