Query Active directory to get the email property o

2020-06-17 05:21发布

问题:

I am doing some querying in active directory at the moment, our database user id matches that of the active directory user id.

I am passing the user id along with the domain and the path to get what I need. My endeavour is to get the email address of the manager from the passed user id. What I am returning when I get the manager property is the distinguished name.

Finding a user's manager record in Active Directory

This above post is my exact problem, but it's an old post and there are no further descriptives on how to move forward and the OP knew what to do next with the distinguished name. Truth is, I don't.

So my question is, how to I get the email address property from the distinguished name which I have thus far stored as a string with a prefix of LDAP:// + "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();
        }
    }

Above is the method I use to get the data I need. Any input or improvements would be appreciated.

Thanks

THIS IS MY SOLUTION FOR THOSE THAT 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();
        }

回答1:

There is no "magic" shortcut to getting the e-mail of a manager.

Once you've retrieved the DN (distinguished name) of your manager (in a string variable called managerDN), you need to again bind to Active Directory by creating another instance of a DirectoryEntry to grab the manager's user info.

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's e-mail  
     return string.Empty;
}